Part Number Hot Search : 
2SC2767 1H220 ALN2250 SF1604GD PD70F HCTS20MS 12001 7SAS31
Product Description
Full Text Search
 

To Download ATTINY2507 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  features ? high performance, low power avr ? 8-bit microcontroller ? advanced risc architecture ? 120 powerful instructions ? most single clock cycl e execution ? 32 x 8 general purpose working registers ? fully static operation ? non-volatile program and data memories ? 2/4/8k byte of in-system programmable program memo ry flash (attiny25/45/85) ? endurance: 10,000 write/erase cycles ? 128/256/512 bytes in-system programmable eeprom (a ttiny25/45/85) ? endurance: 100,000 write/erase cycles ? 128/256/512 bytes internal sram (attiny25/45/85) ? programming lock for self-programming flash progra m and eeprom data security ? peripheral features ? 8-bit timer/counter with prescaler and two pwm cha nnels ? 8-bit high speed timer/counter with separate presc aler ? 2 high frequency pwm outputs with separate output compare registers ? programmable dead time generator ? universal serial interface with start condition de tector ? 10-bit adc ? 4 single ended channels ? 2 differential adc channel pairs with programmable gain (1x, 20x) ? programmable watchdog timer with separate on-chip oscillator ? on-chip analog comparator ? special microcontroller features ? debugwire on-chip debug system ? in-system programmable via spi port ? external and internal interrupt sources ? low power idle, adc noise reduction, and power-dow n modes ? enhanced power-on reset circuit ? programmable brown-out detection circuit ? internal calibrated oscillator ? i/o and packages ? six programmable i/o lines ? 8-pin soic ? 20-pin qfn ? operating voltage ? 2.7 - 5.5v for attiny25/45/85 ? speed grade ? attiny25/45/85: 0 - 8 mhz @ 2.7 - 5.5v, 0 - 16 mhz @ 4.5 - 5.5v ? automotive temperature range ? -40c to +125c ? low power consumption ? active mode: ? 1 mhz, 2.7v: 500 a ? power-down mode: ? 0.2 a at 2.7v 8-bit microcontroller with 2/4/8k bytes in-system programmable flash attiny25 attiny45 attiny85 automotive 7598d?avr?02/07
2 7598d?avr?02/07 attiny25/45/85 auto 1. pin configurations figure 1-1. pinout attiny25/45/85 2. overview the attiny25/45/85 is a low-power cmos 8-bit microc ontroller based on the avr enhanced risc architecture. by executing powerful instructio ns in a single clock cycle, the attiny25/45/85 achieves throughputs approaching 1 mips per mhz all owing the system designer to optimize power consumption versus processing speed. 1 2 3 4 8 7 6 5 (pcint5/reset/adc0/dw) pb5 (pcint3/xtal1/oc1b/adc3) pb3 (pcint4/xtal2/clko/oc1b/adc2) pb4 gnd vcc pb2 (sck/usck/scl/adc1/t0/int0/pcint2) pb1 (miso/do/ain1/oc0b/oc1a/pcint1) pb0 (mosi/di/sda/ain0/oc0a/oc1a/aref/pcint0) soic
3 7598d?avr?02/07 attiny25/45/85 auto 2.1 block diagram figure 2-1. block diagram the avr core combines a rich instruction set with 3 2 general purpose working registers. all the 32 registers are directly connected to the arithmet ic logic unit (alu), allowing two independent registers to be accessed in one single instruction executed in one clock cycle. the resulting architecture is more code efficient while achieving throughputs up to ten times faster than con- ventional cisc microcontrollers. the attiny25/45/85 provides the following features: 2/4/8k byte of in-system programmable flash, 128/256/512 bytes eeprom, 128/256/256 bytes sram, 6 general purpose i/o lines, 32 general purpose working registers, one 8-bit timer/ counter with compare modes, one 8-bit high program counter internal oscilla tor watchdog timer stack pointer program flash sram mcu control register general purpose registers instruction register timer/ counter0 serial universal interface timer/ counter1 instruction decoder data dir. reg.port b data register port b programming logic timing and control mcu status register status register alu port b drivers pb0-pb5 vcc gnd control lines 8-bit databus z adc / analog comparator interrupt unit data eeprom calibrated oscillators y x reset
4 7598d?avr?02/07 attiny25/45/85 auto speed timer/counter, universal serial interface, in ternal and external interrupts, a 4-channel, 10-bit adc, a programmable watchdog timer with inte rnal oscillator, and three software select- able power saving modes. the idle mode stops the cp u while allowing the sram, timer/counter, adc, analog comparator, and interrup t system to continue functioning. the power-down mode saves the register contents, disabl ing all chip functions until the next inter- rupt or hardware reset. the adc noise reduction mod e stops the cpu and all i/o modules except adc, to minimize switching noise during adc conversions. the device is manufactured using atmel?s high densi ty non-volatile memory technology. the on-chip isp flash allows the program memory to be r e-programmed in-system through an spi serial interface, by a conventional non-volatile me mory programmer or by an on-chip boot code running on the avr core. the attiny25/45/85 avr is supported with a full sui te of program and system development tools including: c compilers, macro assemblers, program d ebugger/simulators, in-circuit emulators, and evaluation kits. 2.2 automotive quality grade the attiny25/45/85 have been developed and manufact ured according to the most stringent requirements of the international standard iso-ts-1 6949. this data sheet contains limit values extracted from the results of extensive characteriz ation (temperature and voltage). the quality and reliability of the attiny25/45/85 have been ver ified during regular product qualification as per aec-q100 grade 1. as indicated in the ordering information paragraph, the products are available in three different temperature grades, but with equivalent quality and reliability objectives. different temperature identifiers have been defined as listed in table 2-1 . table 2-1. temperature grade identification for automotive pro ducts temperature temperature identifier comments -40 ; +85 t similar to industrial temperature grade but with au tomotive quality -40 ; +105 t1 reduced automotive temperature range -40 ; +125 z full automotivetemperature range
5 7598d?avr?02/07 attiny25/45/85 auto 2.3 pin descriptions 2.3.1 vcc supply voltage. 2.3.2 gnd ground. 2.3.3 port b (pb5..pb0) port b is a 6-bit bi-directional i/o port with inte rnal pull-up resistors (selected for each bit). the port b output buffers have symmetrical drive charac teristics with both high sink and source capability. as inputs, port b pins that are externa lly pulled low will source current if the pull-up resistors are activated. the port b pins are tri-st ated when a reset condition becomes active, even if the clock is not running. port b also serves the functions of various special features of the attiny25/45/85 as listed on page 54 . on the attiny25 device the programmable i/o ports p b3 and pb4 (pins 2 and 3) are exchanged in the attiny15 compatibility mode for supporting t he backward compatibility with attiny15. 2.3.4 reset reset input. a low level on this pin for longer tha n the minimum pulse length will generate a reset, even if the clock is not running. the minimu m pulse length is given in table 8-1 on page 36 . shorter pulses are not guaranteed to generate a r eset. 3. about code examples this documentation contains simple code examples th at briefly show how to use various parts of the device. these code examples assume that the par t specific header file is included before compilation. be aware that not all c compiler vendo rs include bit definitions in the header files and interrupt handling in c is compiler dependent. please confirm with the c compiler documen- tation for more details. 4. avr cpu core 4.1 introduction this section discusses the avr core architecture in general. the main function of the cpu core is to ensure correct program execution. the cpu mus t therefore be able to access memories, perform calculations, control peripherals, and hand le interrupts.
6 7598d?avr?02/07 attiny25/45/85 auto 4.2 architectural overview figure 4-1. block diagram of the avr architecture in order to maximize performance and parallelism, t he avr uses a harvard architecture ? with separate memories and buses for program and data. i nstructions in the program memory are executed with a single level pipelining. while one instruction is being executed, the next instruc- tion is pre-fetched from the program memory. this c oncept enables instructions to be executed in every clock cycle. the program memory is in-syst em reprogrammable flash memory. the fast-access register file contains 32 x 8-bit g eneral purpose working registers with a single clock cycle access time. this allows single-cycle a rithmetic logic unit (alu) operation. in a typ- ical alu operation, two operands are output from th e register file, the operation is executed, and the result is stored back in the register file ? in one clock cycle. six of the 32 registers can be used as three 16-bit indirect address register pointers for data space addressing ? enabling efficient address calcu lations. one of the these address pointers can also be used as an address pointer for look up tables in flash program memory. these added function registers are the 16-bit x-, y-, and z-register, described later in this section. flash program memory instruction register instruction decoder program counter control lines 32 x 8 general purpose registrers alu status and control i/o lines eeprom data bus 8-bit data sram direct addressing indirect addressing interrupt unit watchdog timer analog comparator i/o module 2 i/o module1 i/o module n
7 7598d?avr?02/07 attiny25/45/85 auto the alu supports arithmetic and logic operations be tween registers or between a constant and a register. single register operations can also be executed in the alu. after an arithmetic opera- tion, the status register is updated to reflect inf ormation about the result of the operation. program flow is provided by conditional and uncondi tional jump and call instructions, able to directly address the whole address space. most avr instructions have a single 16-bit word for- mat. every program memory address contains a 16- or 32-bit instruction. during interrupts and subroutine calls, the return address program counter (pc) is stored on the stack. the stack is effectively allocated in the ge neral data sram, and consequently the stack size is only limited by the total sram size and the usage of the sram. all user programs must initialize the sp in the reset routine (before subr outines or interrupts are executed). the stack pointer (sp) is read/write accessible in the i/o sp ace. the data sram can easily be accessed through the five different addressing modes support ed in the avr architecture. the memory spaces in the avr architecture are all linear and regular memory maps . a flexible interrupt module has its control registe rs in the i/o space with an additional global interrupt enable bit in the status register. all in terrupts have a separate interrupt vector in the interrupt vector table. the interrupts have priorit y in accordance with their interrupt vector posi- tion. the lower the interrupt vector address, the h igher the priority. the i/o memory space contains 64 addresses for cpu peripheral functions as control regis- ters, spi, and other i/o functions. the i/o memory can be accessed directly, or as the data space locations following those of the register fil e, 0x20 - 0x5f. 4.3 alu ? arithmetic logic unit the high-performance avr alu operates in direct con nection with all the 32 general purpose working registers. within a single clock cycle, ari thmetic operations between general purpose registers or between a register and an immediate ar e executed. the alu operations are divided into three main categories ? arithmetic, logical, a nd bit-functions. some implementations of the architecture also provide a powerful multiplier sup porting both signed/unsigned multiplication and fractional format. see the ?instruction set? se ction for a detailed description. 4.4 status register the status register contains information about the result of the most recently executed arith- metic instruction. this information can be used for altering program flow in order to perform conditional operations. note that the status regist er is updated after all alu operations, as specified in the instruction set reference. this wi ll in many cases remove the need for using the dedicated compare instructions, resulting in faster and more compact code. the status register is not automatically stored whe n entering an interrupt routine and restored when returning from an interrupt. this must be hand led by software. the avr status register ? sreg ? is defined as: ? bit 7 ? i: global interrupt enable the global interrupt enable bit must be set for the interrupts to be enabled. the individual inter- rupt enable control is then performed in separate c ontrol registers. if the global interrupt enable bit 7 6 5 4 3 2 1 0 i t h s v n z c sreg read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
8 7598d?avr?02/07 attiny25/45/85 auto register is cleared, none of the interrupts are ena bled independent of the individual interrupt enable settings. the i-bit is cleared by hardware a fter an interrupt has occurred, and is set by the reti instruction to enable subsequent interrupt s. the i-bit can also be set and cleared by the application with the sei and cli instructions, as described in the instruction set reference. ? bit 6 ? t: bit copy storage the bit copy instructions bld (bit load) and bst (b it store) use the t-bit as source or desti- nation for the operated bit. a bit from a register in the register file can be copied into t by the bst instruction, and a bit in t can be copied into a bit in a register in the register file by the bld instruction. ? bit 5 ? h: half carry flag the half carry flag h indicates a half carry in som e arithmetic operations. half carry is useful in bcd arithmetic. see the ?instruction set descrip tion? for detailed information. ? bit 4 ? s: sign bit, s = n v the s-bit is always an exclusive or between the neg ative flag n and the two?s complement overflow flag v. see the ?instruction set descripti on? for detailed information. ? bit 3 ? v: two?s complement overflow flag the two?s complement overflow flag v supports two?s complement arithmetics. see the ?instruction set description? for detailed informat ion. ? bit 2 ? n: negative flag the negative flag n indicates a negative result in an arithmetic or logic operation. see the ?instruction set description? for detailed informat ion. ? bit 1 ? z: zero flag the zero flag z indicates a zero result in an arith metic or logic operation. see the ?instruction set description? for detailed information. ? bit 0 ? c: carry flag the carry flag c indicates a carry in an arithmetic or logic operation. see the ?instruction set description? for detailed information.
9 7598d?avr?02/07 attiny25/45/85 auto 4.5 general purpose register file the register file is optimized for the avr enhanced risc instruction set. in order to achieve the required performance and flexibility, the follo wing input/output schemes are supported by the register file: ? one 8-bit output operand and one 8-bit result inpu t ? two 8-bit output operands and one 8-bit result inp ut ? two 8-bit output operands and one 16-bit result in put ? one 16-bit output operand and one 16-bit result in put figure 4-2 shows the structure of the 32 general purpose work ing registers in the cpu. figure 4-2. avr cpu general purpose working registers most of the instructions operating on the register file have direct access to all registers, and most of them are single cycle instructions. as shown in figure 4-2 , each register is also assigned a data memory addr ess, mapping them directly into the first 32 locations of the user da ta space. although not being physically imple- mented as sram locations, this memory organization provides great flexibility in access of the registers, as the x-, y- and z-pointer registers ca n be set to index any register in the file. 4.5.1 the x-register, y-register, and z-register the registers r26..r31 have some added functions to their general purpose usage. these reg- isters are 16-bit address pointers for indirect add ressing of the data space. the three indirect address registers x, y, and z are defined as descri bed in figure 4-3 . figure 4-3. the x-, y-, and z-registers 7 0 addr. r0 0x00 r1 0x01 r2 0x02 ? r13 0x0d general r14 0x0e purpose r15 0x0f working r16 0x10 registers r17 0x11 ? r26 0x1a x-register low byte r27 0x1b x-register high byte r28 0x1c y-register low byte r29 0x1d y-register high byte r30 0x1e z-register low byte r31 0x1f z-register high byte 15 xh xl 0 x-register 7 0 7 0 r27 (0x1b) r26 (0x1a) 15 yh yl 0
10 7598d?avr?02/07 attiny25/45/85 auto in the different addressing modes these address reg isters have functions as fixed displacement, automatic increment, and automatic decrement (see t he instruction set reference for details). 4.6 stack pointer the stack is mainly used for storing temporary data , for storing local variables and for storing return addresses after interrupts and subroutine ca lls. the stack pointer register always points to the top of the stack. note that the stack is imp lemented as growing from higher memory loca- tions to lower memory locations. this implies that a stack push command decreases the stack pointer. the stack pointer points to the data sram stack are a where the subroutine and interrupt stacks are located. this stack space in the data sr am must be defined by the program before any subroutine calls are executed or interrupts are enabled. the stack pointer must be set to point above 0x60. the stack pointer is decremented by one when data is pushed onto the stack with the push instruction, and it is decremented by two when the return address is pushed onto the stack with subroutine call or interrupt. the st ack pointer is incremented by one when data is popped from the stack with the pop instruction, and it is incremented by two when data is popped from the stack with return from subroutine r et or return from interrupt reti. the avr stack pointer is implemented as two 8-bit r egisters in the i/o space. the number of bits actually used is implementation dependent. not e that the data space in some implementa- tions of the avr architecture is so small that only spl is needed. in this case, the sph register will not be present. 4.7 instruction execution timing this section describes the general access timing co ncepts for instruction execution. the avr cpu is driven by the cpu clock clk cpu , directly generated from the selected clock source for the chip. no internal clock division is used. figure 4-4 shows the parallel instruction fetches and instruc tion executions enabled by the har- vard architecture and the fast access register file concept. this is the basic pipelining concept to obtain up to 1 mips per mhz with the correspondi ng unique results for functions per cost, functions per clocks, and functions per power-unit. y-register 7 0 7 0 r29 (0x1d) r28 (0x1c) 15 zh zl 0 z-register 7 0 7 0 r31 (0x1f) r30 (0x1e) bit 15 14 13 12 11 10 9 8 sp15 sp14 sp13 sp12 sp11 sp10 sp9 sp8 sph sp7 sp6 sp5 sp4 sp3 sp2 sp1 sp0 spl 7 6 5 4 3 2 1 0 read/write r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 1 0 0 1 1 1 1 1
11 7598d?avr?02/07 attiny25/45/85 auto figure 4-4. the parallel instruction fetches and instruction ex ecutions figure 4-5 shows the internal timing concept for the register file. in a single clock cycle an alu operation using two register operands is executed, and the result is stored back to the destina- tion register. figure 4-5. single cycle alu operation 4.8 reset and interrupt handling the avr provides several different interrupt source s. these interrupts and the separate reset vector each have a separate program vector in the p rogram memory space. all interrupts are assigned individual enable bits which must be writt en logic one together with the global interrupt enable bit in the status register in order to enabl e the interrupt. the lowest addresses in the program memory space ar e by default defined as the reset and interrupt vectors. the complete list of vectors is shown in ?interrupts? on page 45 . the list also determines the priority levels of the different int errupts. the lower the address the higher is the priority level. reset has the highest priority, and next is int0 ? the external interrupt request 0. when an interrupt occurs, the global interrupt enab le i-bit is cleared and all interrupts are dis- abled. the user software can write logic one to the i-bit to enable nested interrupts. all enabled interrupts can then interrupt the current interrupt routine. the i-bit is automatically set when a return from interrupt instruction ? reti ? is execu ted. there are basically two types of interrupts. the fi rst type is triggered by an event that sets the interrupt flag. for these interrupts, the program c ounter is vectored to the actual interrupt vec- tor in order to execute the interrupt handling rout ine, and hardware clears the corresponding interrupt flag. interrupt flags can also be cleared by writing a logic one to the flag bit position(s) to be cleared. if an interrupt condition occurs whi le the corresponding interrupt enable bit is clk 1st instruction fetch 1st instruction execute 2nd instruction fetch 2nd instruction execute 3rd instruction fetch 3rd instruction execute 4th instruction fetch t1 t2 t3 t4 cpu total execution time register operands fetch alu operation execute result write back t1 t2 t3 t4 clk cpu
12 7598d?avr?02/07 attiny25/45/85 auto cleared, the interrupt flag will be set and remembe red until the interrupt is enabled, or the flag is cleared by software. similarly, if one or more inte rrupt conditions occur while the global interrupt enable bit is cleared, the corresponding interrupt flag(s) will be set and remembered until the global interrupt enable bit is set, and will then b e executed by order of priority. the second type of interrupts will trigger as long as the interrupt condition is present. these interrupts do not necessarily have interrupt flags. if the interrupt condition disappears before the interrupt is enabled, the interrupt will not be tri ggered. when the avr exits from an interrupt, it will alway s return to the main program and execute one more instruction before any pending interrupt is se rved. note that the status register is not automatically stored when entering an interrupt routine, nor restored when returning from an interrupt routine. this must be handled by software. when using the cli instruction to disable interrupt s, the interrupts will be immediately disabled. no interrupt will be executed after the cli instruc tion, even if it occurs simultaneously with the cli instruction. the following example shows how th is can be used to avoid interrupts during the timed eeprom write sequence.. when using the sei instruction to enable interrupts , the instruction following sei will be exe- cuted before any pending interrupts, as shown in th is example. assembly code example in r16, sreg ; store sreg value cli ; disable interrupts during timed sequence sbi eecr, eemwe ; start eeprom write sbi eecr, eewe out sreg, r16 ; restore sreg value (i-bit) c code example char csreg; csreg = sreg; /* store sreg value */ /* disable interrupts during timed sequence */ _cli(); eecr |= (1< 13 7598d?avr?02/07 attiny25/45/85 auto 4.8.1 interrupt response time the interrupt execution response for all the enable d avr interrupts is four clock cycles mini- mum. after four clock cycles the program vector add ress for the actual interrupt handling routine is executed. during this four clock cycle period, t he program counter is pushed onto the stack. the vector is normally a jump to the interrupt rout ine, and this jump takes three clock cycles. if an interrupt occurs during execution of a multi-cyc le instruction, this instruction is completed before the interrupt is served. if an interrupt occ urs when the mcu is in sleep mode, the interrupt execution response time is increased by four clock cycles. this increase comes in addition to the start-up time from the selected sleep mode. a return from an interrupt handling routine takes f our clock cycles. during these four clock cycles, the program counter (two bytes) is popped b ack from the stack, the stack pointer is incremented by two, and the i-bit in sreg is set. 5. avr attiny25/45/85 memories this section describes the different memories in th e attiny25/45/85. the avr architecture has two main memory spaces, the data memory and the pro gram memory space. in addition, the attiny25/45/85 features an eeprom memory for data s torage. all three memory spaces are lin- ear and regular. 5.1 in-system re-programmable flash program memory the attiny25/45/85 contains 2/4/8k byte on-chip in- system reprogrammable flash memory for program storage. since all avr instructions are 16 or 32 bits wide, the flash is organized as 1024/2049/4096 x 16. the flash memory has an endurance of at least 10,00 0 write/erase cycles. the attiny25/45/85 program counter (pc) is 10/11/12 bits wide, thus ad dressing the 1024/2048/4096 program memory locations. ?memory programming? on page 139 contains a detailed description on flash data serial downloading using the spi pins. constant tables can be allocated within the entire program memory address space (see the lpm ? load program memory instruction description). timing diagrams for instruction fetch and execution are presented in ?instruction execution tim- ing? on page 10 . figure 5-1. program memory map 0x0000 0x03ff/0x07ff program memory
14 7598d?avr?02/07 attiny25/45/85 auto 5.2 sram data memory figure 5-2 shows how the attiny25/45/85 sram memory is organi zed. the lower 224/352/607 data memory locations address both the register file, the i/o memory and the internal data sram. the first 32 locations address the register file, the next 64 loca- tions the standard i/o memory, and the last 128/256 /512 locations address the internal data sram. the five different addressing modes for the data me mory cover: direct, indirect with displace- ment, indirect, indirect with pre-decrement, and in direct with post-increment. in the register file, registers r26 to r31 feature the indirect add ressing pointer registers. the direct addressing reaches the entire data space . the indirect with displacement mode reaches 63 addr ess locations from the base address given by the y- or z-register. when using register indirect addressing modes with automatic pre-decrement and post-incre- ment, the address registers x, y, and z are decreme nted or incremented. the 32 general purpose working registers, 64 i/o re gisters, and the 128/256/512 bytes of inter- nal data sram in the attiny25/45/85 are all accessi ble through all these addressing modes. the register file is described in ?general purpose register file? on page 9 . figure 5-2. data memory map 5.2.1 data memory access times this section describes the general access timing co ncepts for internal memory access. the internal data sram access is performed in two clk cpu cycles as described in figure 5-3 . 32 registers 64 i/o registers internal sram (128/256/512 x 8) 0x0000 - 0x001f0x0020 - 0x005f 0x0df/0x015f/0x025f 0x0060 data memory
15 7598d?avr?02/07 attiny25/45/85 auto figure 5-3. on-chip data sram access cycles 5.3 eeprom data memory the attiny25/45/85 contains 128/256/512 bytes of da ta eeprom memory. it is organized as a separate data space, in which single bytes can be r ead and written. the eeprom has an endurance of at least 100,000 write/erase cycles. t he access between the eeprom and the cpu is described in the following, specifying the e eprom address registers, the eeprom data register, and the eeprom control register. for a detailed description of serial data downloading to the eeprom, see page 143 . 5.3.1 eeprom read/write access the eeprom access registers are accessible in the i /o space. the write access times for the eeprom are given in table 5-1 . a self-timing function, however, lets the user software detect when the next byte ca n be written. if the user code contains instruc- tions that write the eeprom, some precautions must be taken. in heavily filtered power supplies, v cc is likely to rise or fall slowly on power-up/down. this causes the device for some period of time to run at a voltage lower than speci fied as minimum for the clock frequency used. see ?preventing eeprom corruption? on page 20 for details on how to avoid problems in these situations. in order to prevent unintentional eeprom writes, a specific write procedure must be followed. refer to ?atomic byte programming? on page 17 and ?split byte programming? on page 18 for details on this. when the eeprom is read, the cpu is halted for four clock cycles before the next instruction is executed. when the eeprom is written, the cpu is ha lted for two clock cycles before the next instruction is executed. 5.3.2 eeprom address register high ? eearh clk wr rd datadata address address valid t1 t2 t3 compute address read write cpu memory access instruction next instruction bit 7 6 5 4 3 2 1 0 - - - - - - - eear8 eearh read/write r r r r r r r r/w initial value x x x x x x x x
16 7598d?avr?02/07 attiny25/45/85 auto ? bit 7..1 ? res6..0: reserved bits these bits are reserved for future use and will alw ays read as 0 in attiny25/45/85. ? bits 0 ? eear8: eeprom address the eeprom address register ? eearh ? specifies the high eeprom address in the 128/256/512 bytes eeprom space. the eeprom data byt es are addressed linearly between 0 and 127/255/511. the initial value of eear is undef ined. a proper value must be written before the eeprom may be accessed. 5.3.3 eeprom address register ? eearl ? bits 7..0 ? eear7..0: eeprom address the eeprom address register ? eearl ? specifies the low eeprom address in the 128/256/512 bytes eeprom space. the eeprom data byt es are addressed linearly between 0 and 127/255/511. the initial value of eear is undef ined. a proper value must be written before the eeprom may be accessed. 5.3.4 eeprom data register ? eedr ? bits 7..0 ? eedr7..0: eeprom data for the eeprom write operation the eedr register co ntains the data to be written to the eeprom in the address given by the eear register. f or the eeprom read operation, the eedr contains the data read out from the eeprom at the address given by eear. 5.3.5 eeprom control register ? eecr ? bit 7 ? res: reserved bit this bit is reserved for future use and will always read as 0 in attiny25/45/85. for compatibility with future avr devices, always write this bit to z ero. after reading, mask out this bit. ? bit 6 ? res: reserved bit this bit is reserved in the attiny25/45/85 and will always read as zero. ? bits 5, 4 ? eepm1 and eepm0: eeprom programming mo de bits the eeprom programming mode bits setting defines wh ich programming action that will be triggered when writing eepe. it is possible to prog ram data in one atomic operation (erase the old value and program the new value) or to split th e erase and write operations in two different bit 7 6 5 4 3 2 1 0 eear7 eear6 eear5 eear4 eear3 eear2 eear1 eear0 eearl read/write r r r/w r/w r/w r/w r/w r/w initial value x x x x x x x x bit 7 6 5 4 3 2 1 0 eedr7 eedr6 eedr5 eedr4 eedr3 eedr2 eedr1 eedr0 eedr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value x x x x x x x x bit 7 6 5 4 3 2 1 0 ? ? eepm1 eepm0 eerie eempe eepe eere eecr read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 x x 0 0 x 0
17 7598d?avr?02/07 attiny25/45/85 auto operations. the programming times for the different modes are shown in table 5-1 . while eepe is set, any write to eepmn will be ignored. during reset, the eepmn bits will be reset to 0b00 unless the eeprom is busy programming. ? bit 3 ? eerie: eeprom ready interrupt enable writing eerie to one enables the eeprom ready inter rupt if the i-bit in sreg is set. writing eerie to zero disables the interrupt. the eeprom re ady interrupt generates a constant inter- rupt when non-volatile memory is ready for programm ing. ? bit 2 ? eempe: eeprom master program enable the eempe bit determines whether writing eepe to on e will have effect or not. when eempe is set, setting eepe within four clock c ycles will program the eeprom at the selected address. if eempe is zero, setting eepe wi ll have no effect. when eempe has been written to one by software, hardware clears the bit to zero after four clock cycles. ? bit 1 ? eepe: eeprom program enable the eeprom program enable signal eepe is the progra mming enable signal to the eeprom. when eepe is written, the eeprom will be programmed according to the eepmn bits setting. the eempe bit must be written to one before a logic al one is written to eepe, otherwise no eeprom write takes place. when the write access ti me has elapsed, the eepe bit is cleared by hardware. when eepe has been set, the cpu is hal ted for two cycles before the next instruction is executed. ? bit 0 ? eere: eeprom read enable the eeprom read enable signal ? eere ? is the read strobe to the eeprom. when the cor- rect address is set up in the eear register, the ee re bit must be written to one to trigger the eeprom read. the eeprom read access takes one instr uction, and the requested data is available immediately. when the eeprom is read, the cpu is halted for four cycles before the next instruction is executed. the user should poll the eepe bit before starting the read opera- tion. if a write operation is in progress, it is ne ither possible to read the eeprom, nor to change the eear register. 5.3.6 atomic byte programming using atomic byte programming is the simplest mode. when writing a byte to the eeprom, the user must write the address into the eearl register and data into eedr register. if the eepmn bits are zero, writing eepe (within four cycl es after eempe is written) will trigger the erase/write operation. both the erase and write cyc le are done in one operation and the total programming time is given in table 1. the eepe bit remains set until the erase and write opera- tions are completed. while the device is busy with programming, it is not possible to do any other eeprom operations. table 5-1. eeprom mode bits eepm1 eepm0 programming time operation 0 0 3.4 ms erase and write in one operation (atomic op eration) 0 1 1.8 ms erase only 1 0 1.8 ms write only 1 1 ? reserved for future use
18 7598d?avr?02/07 attiny25/45/85 auto 5.3.7 split byte programming it is possible to split the erase and write cycle i n two different operations. this may be useful if the system requires short access time for some limi ted period of time (typically if the power sup- ply voltage falls). in order to take advantage of t his method, it is required that the locations to be written have been erased before the write operation . but since the erase and write operations are split, it is possible to do the erase operation s when the system allows doing time-critical operations (typically after power-up). 5.3.8 erase to erase a byte, the address must be written to eea r. if the eepmn bits are 0b01, writing the eepe (within four cycles after eempe is written) w ill trigger the erase operation only (program- ming time is given in table 1). the eepe bit remain s set until the erase operation completes. while the device is busy programming, it is not pos sible to do any other eeprom operations. 5.3.9 write to write a location, the user must write the addres s into eear and the data into eedr. if the eepmn bits are 0b10, writing the eepe (within four cycles after eempe is written) will trigger the write operation only (programming time is give n in table 1). the eepe bit remains set until the write operation completes. if the location to b e written has not been erased before write, the data that is stored must be considered as lost. whi le the device is busy with programming, it is not possible to do any other eeprom operations. the calibrated oscillator is used to time the eepro m accesses. make sure the oscillator fre- quency is within the requirements described in ?oscillator calibration register ? osccal? on page 27 . the following code examples show one assembly and o ne c function for erase, write, or atomic write of the eeprom. the examples assume that inter rupts are controlled (e.g., by disabling interrupts globally) so that no interrupts will occ ur during execution of these functions
19 7598d?avr?02/07 attiny25/45/85 auto . assembly code example eeprom_write: ; wait for completion of previous write sbic eecr,eepe rjmp eeprom_write ; set programming mode ldi r16, (0<>eepm0) /* set up address and data registers */ eearl = ucaddress; eedr = ucdata; /* write logical one to eemwe */ eecr |= (1< 20 7598d?avr?02/07 attiny25/45/85 auto the next code examples show assembly and c function s for reading the eeprom. the exam- ples assume that interrupts are controlled so that no interrupts will occur during execution of these functions. 5.3.10 preventing eeprom corruption during periods of low v cc , the eeprom data can be corrupted because the supp ly voltage is too low for the cpu and the eeprom to operate prope rly. these issues are the same as for board level systems using eeprom, and the same desi gn solutions should be applied. an eeprom data corruption can be caused by two situ ations when the voltage is too low. first, a regular write sequence to the eeprom requires a m inimum voltage to operate correctly. sec- ondly, the cpu itself can execute instructions inco rrectly, if the supply voltage is too low. eeprom data corruption can easily be avoided by fol lowing this design recommendation: keep the avr reset active (low) during periods of i nsufficient power supply voltage. this can be done by enabling the internal brown-out detector (bod). if the detection level of the internal bod does not match the needed detection level, an e xternal low v cc reset protection circuit can be used. if a reset occurs while a write operation is in progress, the write operation will be com- pleted provided that the power supply voltage is su fficient. assembly code example eeprom_read: ; wait for completion of previous write sbic eecr,eepe rjmp eeprom_read ; set up address (r17) in address register out eearl, r17 ; start eeprom read by writing eere sbi eecr,eere ; read data from data register in r16,eedr ret c code example unsigned char eeprom_read( unsigned char ucaddress) { /* wait for completion of previous write */ while(eecr & (1< 21 7598d?avr?02/07 attiny25/45/85 auto 5.4 i/o memory the i/o space definition of the attiny25/45/85 is s hown in ?register summary? on page 188 . all attiny25/45/85 i/os and peripherals are placed in the i/o space. all i/o locations may be accessed by the ld/lds/ldd and st/sts/std instructi ons, transferring data between the 32 general purpose working registers and the i/o space . i/o registers within the address range 0x00 - 0x1f are directly bit-accessible using the s bi and cbi instructions. in these registers, the value of single bits can be checked by using the sb is and sbic instructions. refer to the instruction set section for more details. when usin g the i/o specific commands in and out, the i/o addresses 0x00 - 0x3f must be used. when addres sing i/o registers as data space using ld and st instructions, 0x20 must be added to these addresses. for compatibility with future devices, reserved bit s should be written to zero if accessed. reserved i/o memory addresses should never be writt en. some of the status flags are cleared by writing a l ogical one to them. note that, unlike most other avrs, the cbi and sbi instructions will only operate on the specified bit, and can therefore be used on registers containing such status flags. the cbi and sbi instructions work with reg- isters 0x00 to 0x1f only. the i/o and peripherals control registers are expla ined in later sections. 6. system clock and clock options 6.1 clock systems and their distribution figure 6-1 presents the principal clock systems in the avr an d their distribution. all of the clocks need not be active at a given time. in order to red uce power consumption, the clocks to modules not being used can be halted by using different sle ep modes, as described in ?power manage- ment and sleep modes? on page 31 . the clock systems are detailed below. figure 6-1. clock distribution general i/o modules cpu core ram clk i/o avr clock control unit clk cpu flash and eeprom clk flash source clock watchdog timer watchdog oscillator reset logic clock multiplexer watchdog clock calibrated rc oscillator calibrated rc oscillator external clock adc clk adc crystal oscillator low-frequency crystal oscillator system clock prescaler pll oscillator clk pck clk pck
22 7598d?avr?02/07 attiny25/45/85 auto 6.1.1 cpu clock ? clk cpu the cpu clock is routed to parts of the system conc erned with operation of the avr core. examples of such modules are the general purpose re gister file, the status register and the data memory holding the stack pointer. halting the cpu clock inhibits the core from performing general operations and calculations. 6.1.2 i/o clock ? clk i/o the i/o clock is used by the majority of the i/o mo dules, like timer/counter. the i/o clock is also used by the external interrupt module, but not e that some external interrupts are detected by asynchronous logic, allowing such interrupts to be detected even if the i/o clock is halted. 6.1.3 flash clock ? clk flash the flash clock controls operation of the flash int erface. the flash clock is usually active simul- taneously with the cpu clock. 6.1.4 adc clock ? clk adc the adc is provided with a dedicated clock domain. this allows halting the cpu and i/o clocks in order to reduce noise generated by digital circu itry. this gives more accurate adc conversion results. 6.1.5 internal pll for fast peripheral clock generat ion - clk pck the internal pll in attiny25/45/85 generates a cloc k frequency that is 8x multiplied from a source input. the source of the pll input clock is the output of the internal rc oscillator having a frequency of 8.0 mhz. thus the output of the pll, the fast peripheral clock is 64 mhz. the fast peripheral clock, or a clock prescaled from that, c an be selected as the clock source for timer/counter1. see the figure 6-2 on page 23 . since the attiny25/45/85 device is a migration path for attiny15, there is an attiny15 compati- bility mode for supporting the backward compatibili ty with attiny15. the attiny15 compatibility mode is selected by programming the cksel fuses to ?0011?. in the attiny15 compatibility mode the frequency of the internal rc oscillator is calibrated down to 6.4 mhz and the multipli- cation factor of the pll is set to 4x. with these a djustments the clocking system is attiny15 compatible and the resulting fast peripheral clock has a frequency of 25.6 mhz (same as in attiny15). the pll is locked on the rc oscillator and adjustin g the rc oscillator via osccal register will adjust the fast peripheral clock at the same time. however, even if the rc oscillator is taken to a higher frequency than 8 mhz, the fast peripheral cl ock frequency saturates at 85 mhz (worst case) and remains oscillating at the maximum freque ncy. it should be noted that the pll in this case is not locked any longer with the rc oscillato r clock. therefore, it is recommended not to take the osccal adjustments to a higher frequency than 8 mhz in order to keep the pll in the correct operati ng range. the internal pll is enabled only when the plle bit in the register pllcsr is set or the pllck fuse is programmed (?0?). the bit plock from the register pllcsr is set when pll is l ocked. both internal rc oscillator and pll are switched of f in power down and stand-by sleep modes.
23 7598d?avr?02/07 attiny25/45/85 auto figure 6-2. pck clocking system 6.2 clock sources the device has the following clock source options, selectable by flash fuse bits as shown below. the clock from the selected source is input to the avr clock generator, and routed to the appropriate modules. note: 1. for all fuses ?1? means unprogrammed while ? 0? means programmed. the various choices for each clocking option is giv en in the following sections. when the cpu wakes up from power-down or power-save, the selecte d clock source is used to time the start- up, ensuring stable oscillator operation before ins truction execution starts. when the cpu starts from reset, there is an additional delay allowing t he power to reach a stable level before com- mencing normal operation. the watchdog oscillator i s used for timing this real-time part of the start-up time. the number of wdt oscillator cycles used for each time-out is shown in table 6- 2 . 8.0 mhz / 6.4 mhz rc oscillator osccal xtal1 xtal2 oscillators divide by 4 system clock pll 8x / 4x pllck & cksel fuses plle pck lock detector plock 64 / 25.6 mhz system clock prescaler clkps3..0 table 6-1. device clocking options select (1) device clocking option cksel3..0 external clock 0000 pll clock 0001 calibrated internal rc oscillator 8.0 mhz 0010 calibrated internal rc oscillator 6.4 mhz 0011 watchdog oscillator 128 khz 0100 external low-frequency oscillator 0110 external crystal/ceramic resonator 1000-1111 reserved 0101, 0111
24 7598d?avr?02/07 attiny25/45/85 auto 6.3 default clock source the device is shipped with cksel = ?0010?, sut = ?1 0?, and ckdiv8 programmed. the default clock source setting is therefore the internal rc o scillator running at 8 mhz with longest start-up time and an initial system clock prescaling of 8. t his default setting ensures that all users can make their desired clock source setting using an in -system or high-voltage programmer. 6.4 crystal oscillator xtal1 and xtal2 are input and output, respectively, of an inverting amplifier which can be con- figured for use as an on-chip oscillator, as shown in figure 6-3 . either a quartz crystal or a ceramic resonator may be used. c1 and c2 should always be equal for both crystals and resonators. the optimal value of the capacitors depends on the crystal or resonator in u se, the amount of stray capacitance, and the electromagnetic noise of the environment. some init ial guidelines for choosing capacitors for use with crystals are given in table 6-3 . for ceramic resonators, the capacitor values give n by the manufacturer should be used. figure 6-3. crystal oscillator connections the oscillator can operate in three different modes , each optimized for a specific frequency range. the operating mode is selected by the fuses cksel3..1 as shown in table 6-3 . notes: 1. this option should not be used with crystal s, only with ceramic resonators. the cksel0 fuse together with the sut1..0 fuses sel ect the start-up times as shown in table 6-4 . table 6-2. number of watchdog oscillator cycles typ time-out number of cycles 4 ms 512 64 ms 8k (8,192) table 6-3. crystal oscillator operating modes cksel3..1 frequency range (mhz) recommended range for capacitors c1 and c2 for use with crystals (pf) 100 (1) 0.4 - 0.9 ? 101 0.9 - 3.0 12 - 22 110 3.0 - 8.0 12 - 22 111 8.0 - 12 - 22 xtal2xtal1 gnd c2 c1
25 7598d?avr?02/07 attiny25/45/85 auto notes: 1. these options should only be used when not operating close to the maximum frequency of the device, and only if frequency stability at start-up is not important for the application. these options are not suitable for crystals. 2. these options are intended for use with ceramic r esonators and will ensure frequency stability at start-up. they can also be used with crystals wh en not operating close to the maximum fre- quency of the device, and if frequency stability at start-up is not important for the application. 6.5 low-frequency crystal oscillator to use a 32.768 khz watch crystal as the clock sour ce for the device, the low-frequency crystal oscillator must be selected by setting cksel fuses to ?0110?. the crystal should be connected as shown in figure 6-3 . refer to the 32 khz crystal oscillator applicatio n note for details on oscillator operation and how to choose appropriate values for c1 and c2. when this oscillator is selected, start-up times ar e determined by the sut fuses as shown in table 6-5 . notes: 1. these options should only be used if freque ncy stability at start-up is not important for the application. table 6-4. start-up times for the crystal oscillator clock sel ection cksel0 sut1..0 start-up time from power-down and power-save additional delay from reset (v cc = 5.0v) recommended usage 0 00 258 ck (1) 14ck + 4.1 ms ceramic resonator, fast rising power 0 01 258 ck (1) 14ck + 65 ms ceramic resonator, slowly rising power 0 10 1k ck (2) 14ck ceramic resonator, bod enabled 0 11 1k ck (2) 14ck + 4.1 ms ceramic resonator, fast rising power 1 00 1k ck (2) 14ck + 65 ms ceramic resonator, slowly rising power 1 01 16k ck 14ck crystal oscillator, bod enabled 1 10 16k ck 14ck + 4.1 ms crystal oscillator, fast rising power 1 11 16k ck 14ck + 65 ms crystal oscillator, slowly rising power table 6-5. start-up times for the low frequency crystal oscill ator clock selection sut1..0 start-up time from power down and power save additional delay from reset (v cc = 5.0v) recommended usage 00 1k ck (1) 4 ms fast rising power or bod enabled 01 1k ck (1) 64 ms slowly rising power 10 32k ck 64 ms stable frequency at start-up 11 reserved
26 7598d?avr?02/07 attiny25/45/85 auto 6.6 calibrated internal rc oscillator the calibrated internal rc oscillator provides an 8 .0 mhz clock. the frequency is the nominal value at 3v and 25 c. if the frequency exceeds the specification of th e device (depends on v cc ), the ckdiv8 fuse must be programmed in order to divi de the internal frequency by 8 during start-up. see ?system clock prescaler? on page 29. for more details. this clock may be selected as the system clock by programming the cksel fuses as shown in table 6-6 . if selected, it will operate with no external components. during reset, hardware loads the calibration byte into the osccal register and thereby automatically calibrate s the rc oscillator. at 3v and 25 c, this calibration gives a frequency within 1% of the no minal frequency. when this oscillator is used as the chip clock, the watchdog oscillator will sti ll be used for the watchdog timer and for the reset time-out. for more information on the pre-pro grammed calibration value, see the section ?calibration byte? on page 142 . in addition the calibrated internal rc oscillator p rovides a 6.4 mhz clock that is chosen by writ- ing the cksel fuses to ?0011? as shown in table 7. when this cksel setting is written the nominal frequency of the calibrated internal rc osc illator is calibrated down to 6.4 mhz. this clock frequency is needed for the attiny15 compatib ility mode. in the attiny15 compatibility mode the 6.4 mhz internal rc oscillator is used as a reference clock for the pll that is generat- ing a 4x multiplied frequency from the reference cl ock. the resulting frequency is 25.6 mhz and it is needed for supporting the same pwm frequencie s as in attiny15. note: 1. the device is shipped with this option selec ted. when this oscillator is selected, start-up times ar e determined by the sut fuses as shown in table 6-7 and the start-up times in attiny15 compatibility m ode in table 9. note: 1. the device is shipped with this option selec ted. table 6-6. internal calibrated rc oscillator operating modes cksel3..0 nominal frequency 0010 (1) 8.0 mhz 0011 6.4 mhz table 6-7. start-up times for the internal calibrated rc oscil lator clock selection sut1..0 start-up time from power-down additional delay from reset (v cc = 5.0v) recommended usage 00 6 ck 14ck bod enabled 01 6 ck 14ck + 4 ms fast rising power 10 (1) 6 ck 14ck + 64 ms slowly rising power 11 reserved table 6-8. start-up times for the internal calibrated rc oscil lator clock selection sut1..0 start-up time from power-down additional delay from reset (v cc = 5.0v) recommended usage 00 6 ck 14ck + 64 ms 01 6 ck 14ck + 64 ms 10 (1) 6 ck 14ck + 4 ms 11 1 ck 14ck
27 7598d?avr?02/07 attiny25/45/85 auto 6.6.1 oscillator calibration register ? osccal ? bits 7..0 ? cal7..0: oscillator calibration value writing the calibration byte to this address will t rim the internal oscillator to remove process vari- ations from the oscillator frequency. this is done automatically during chip reset. when osccal is zero, the lowest available frequency is c hosen. writing non-zero values to this regis- ter will increase the frequency of the internal osc illator. writing 0xff to the register gives the highest available frequency. the calibrated oscilla tor is used to time eeprom and flash access. if eeprom or flash is written, do not calib rate to more than 8.8 mhz frequency. other- wise, the eeprom or flash write may fail. the cal7 bit determines the range of operation for the oscillator. setting this bit to 0 gives the lowest frequency range, setting this bit to 1 gives the highest frequency range. the two fre- quency ranges are overlapping, in other words a set ting of osccal = 0x7f gives a higher frequency than osccal = 0x80. the cal6..0 bits are used to tune the frequency wit hin the selected range. a setting of 0x00 gives the lowest frequency in that range, and a set ting of 0x7f gives the highest frequency in the range. incrementing cal6..0 by 1 will give a freque ncy increment of less than 2% in the fre- quency range 7.3 - 8.1 mhz. avoid changing the calibration value in large steps when calibrating the calibrated internal rc oscillator to ensure stable operation of the mcu. a variation in frequency of more than 2% from one cycle to the next can lead to unpredicatble beh avior. changes in osccal should not exceed 0x20 for each calibration. it is required to ensure that the mcu is kept in reset during such changes in the clock frequency 6.7 external clock to drive the device from an external clock source, clki should be driven as shown in figure 6- 4 . to run the device on an external clock, the cksel fuses must be programmed to ?00?. figure 6-4. external clock drive configuration bit 7 6 5 4 3 2 1 0 cal7 cal6 cal5 cal4 cal3 cal2 cal1 cal0 osccal read/write r r/w r/w r/w r/w r/w r/w r/w initial value 0 device specific calibration value table 6-9. internal rc oscillator frequency range osccal value min frequency in percentage of nominal frequency max frequency in percentage of nominal frequency 0x00 50% 100% 0x3f 75% 150% 0x7f 100% 200% external clock signal clkignd
28 7598d?avr?02/07 attiny25/45/85 auto when this clock source is selected, start-up times are determined by the sut fuses as shown in table 6-10 . note that the system clock prescaler can be used to implement run-time changes of the internal clock frequency while still ensuring stable operati on. refer to ?system clock prescaler? on page 29 for details. 6.7.1 high frequency pll clock - pll clk there is an internal pll that provides nominally 64 mhz clock rate locked to the rc oscillator for the use of the peripheral timer/counter1 and fo r the system clock source. when selected as a system clock source, by programming the cksel fus es to ?0001?, it is divided by four like shown in table 6-11 . when this clock source is selected, start-up time s are determined by the sut fuses as shown in table 6-12 . see also ?pck clocking system? on page 23 . 6.8 128 khz internal oscillator the 128 khz internal oscillator is a low power osci llator providing a clock of 128 khz. the fre- quency is nominal at 3v and 25 c. this clock may be select as the system clock by programming the cksel fuses to ?11?. table 6-10. start-up times for the external clock selection sut1..0 start-up time from power- down and power-save additional delay from reset recommended usage 00 6 ck 14ck bod enabled 01 6 ck 14ck + 4 ms fast rising power 10 6 ck 14ck + 64 ms slowly rising power 11 reserved table 6-11. pllck operating modes cksel3..0 nominal frequency 0001 16 mhz table 6-12. start-up times for the pllck sut1..0 start-up time from power down and power save additional delay from reset (v cc = 5.0v) recommended usage 00 1k ck 14ck + 8ms bod enabled 01 16k ck 14ck + 8ms fast rising power 10 1k ck 14ck + 68 ms slowly rising power 11 16k ck 14ck + 68 ms slowly rising power
29 7598d?avr?02/07 attiny25/45/85 auto when this clock source is selected, start-up times are determined by the sut fuses as shown in table 6-13 . 6.9 clock output buffer the device can output the system clock on the clko pin. to enable the output, the ckout fuse has to be programmed. this mode is suitable wh en the chip clock is used to drive other cir- cuits on the system. note that the clock will not b e output during reset and the normal operation of i/o pin will be overridden when the fuse is prog rammed. any clock source, including the inter- nal rc oscillator, can be selected when the clock i s output on clko. if the system clock prescaler is used, it is the divided system clock t hat is output. 6.10 system clock prescaler the attiny25/45/85 system clock can be divided by s etting the clock prescale register ? clkpr. this feature can be used to decrease power c onsumption when the requirement for processing power is low. this can be used with all clock source options, and it will affect the clock frequency of the cpu and all synchronous peri pherals. clk i/o , clk adc , clk cpu , and clk flash are divided by a factor as shown in table 6-14 . 6.10.1 clock prescale register ? clkpr ? bit 7 ? clkpce: clock prescaler change enable the clkpce bit must be written to logic one to enab le change of the clkps bits. the clkpce bit is only updated when the other bits in clkpr ar e simultaniosly written to zero. clkpce is cleared by hardware four cycles after it is written or when the clkps bits are written. rewriting the clkpce bit within this time-out period does nei ther extend the time-out period, nor clear the clkpce bit. ? bits 6..4 ? res: reserved bits these bits are reserved bits in the attiny25/45/85 and will always read as zero. ? bits 3..0 ? clkps3..0: clock prescaler select bits 3 - 0 these bits define the division factor between the s elected clock source and the internal system clock. these bits can be written run-time to vary t he clock frequency to suit the application requirements. as the divider divides the master clo ck input to the mcu, the speed of all synchro- nous peripherals is reduced when a division factor is used. the division factors are given in table 6-14 . table 6-13. start-up times for the 128 khz internal oscillator sut1..0 start-up time from power- down and power-save additional delay from reset recommended usage 00 6 ck 14ck bod enabled 01 6 ck 14ck + 4 ms fast rising power 10 6 ck 14ck + 64 ms slowly rising power 11 reserved bit 7 6 5 4 3 2 1 0 clkpce ? ? ? clkps3 clkps2 clkps1 clkps0 clkpr read/write r/w r r r r/w r/w r/w r/w initial value 0 0 0 0 see bit description
30 7598d?avr?02/07 attiny25/45/85 auto to avoid unintentional changes of clock frequency, a special write procedure must be followed to change the clkps bits: 1. write the clock prescaler change enable (clkpce) bit to one and all other bits in clkpr to zero. 2. within four cycles, write the desired value to cl kps while writing a zero to clkpce. interrupts must be disabled when changing prescaler setting to make sure the write procedure is not interrupted. the ckdiv8 fuse determines the initial value of the clkps bits. if ckdiv8 is unprogrammed, the clkps bits will be reset to ?0000?. if ckdiv8 i s programmed, clkps bits are reset to ?0011?, giving a division factor of eight at start up. this feature should be used if the selected clock source has a higher frequency than the maximu m frequency of the device at the present operating conditions. note that any value can be wr itten to the clkps bits regardless of the ckdiv8 fuse setting. the application software must ensure that a sufficient division factor is chosen if the selcted clock source has a higher fre quency than the maximum frequency of the device at the present operating conditions. the dev ice is shipped with the ckdiv8 fuse programmed. 6.10.2 switching time when switching between prescaler settings, the syst em clock prescaler ensures that no glitches occur in the clock system and that no inte rmediate frequency is higher than neither the clock frequency corresponding to the previous setti ng, nor the clock frequency corresponding to the new setting. the ripple counter that implements the prescaler ru ns at the frequency of the undivided clock, which may be faster than the cpu?s clock frequency. hence, it is not possible to determine the table 6-14. clock prescaler select clkps3 clkps2 clkps1 clkps0 clock division factor 0 0 0 0 1 0 0 0 1 2 0 0 1 0 4 0 0 1 1 8 0 1 0 0 16 0 1 0 1 32 0 1 1 0 64 0 1 1 1 128 1 0 0 0 256 1 0 0 1 reserved 1 0 1 0 reserved 1 0 1 1 reserved 1 1 0 0 reserved 1 1 0 1 reserved 1 1 1 0 reserved 1 1 1 1 reserved
31 7598d?avr?02/07 attiny25/45/85 auto state of the prescaler ? even if it were readable, and the exact time it takes to switch from one clock division to another cannot be exactly predict ed. from the time the clkps values are written, it take s between t1 + t2 and t1 + 2*t2 before the new clock frequency is active. in this interval, 2 active clock edges are produced. here, t1 is the previous clock period, and t2 is the period corresp onding to the new prescaler setting. 7. power management and sleep modes the high performance and industry leading code effi ciency makes the avr microcontrollers an ideal choise for low power applications. sleep modes enable the application to shut down unu sed modules in the mcu, thereby saving power. the avr provides various sleep modes allowin g the user to tailor the power consump- tion to the application?s requirements. to enter any of the three sleep modes, the se bit i n mcucr must be written to logic one and a sleep instruction must be executed. the sm1..0 bits in the mcucr register select which sleep mode (idle, adc noise reduction, or power-dow n) will be activated by the sleep instruc- tion. see table 7-1 for a summary. if an enabled interrupt occurs whil e the mcu is in a sleep mode, the mcu wakes up. the mcu is then halted for four cycles in addition to the start-up time, executes the interrupt routine, and resumes executi on from the instruction following sleep. the contents of the register file and sram are unaltere d when the device wakes up from sleep. if a reset occurs during sleep mode, the mcu wakes up an d executes from the reset vector. figure 6-1 on page 21 presents the different clock systems in the attiny 25/45/85, and their dis- tribution. the figure is helpful in selecting an ap propriate sleep mode. 7.0.1 mcu control register ? mcucr the mcu control register contains control bits for power management. ? bit 5 ? se: sleep enable the se bit must be written to logic one to make the mcu enter the sleep mode when the sleep instruction is executed. to avoid the mcu entering the sleep mode unless it is the programmer?s purpose, it is recommended to write the sleep enabl e (se) bit to one just before the execution of the sleep instruction and to clear it immediately a fter waking up. ? bits 4, 3 ? sm1..0: sleep mode select bits 2..0 these bits select between the three available sleep modes as shown in table 7-1 . bit 7 6 5 4 3 2 1 0 ? pud se sm1 sm0 ? isc01 isc00 mcucr read/write r r/w r/w r/w r/w r r/w r/w initial value 0 0 0 0 0 0 0 0 table 7-1. sleep mode select sm1 sm0 sleep mode 0 0 idle 0 1 adc noise reduction 1 0 power-down 1 1 stand-by mode
32 7598d?avr?02/07 attiny25/45/85 auto ? bit 2 ? res: reserved bit this bit is a reserv ed bit in the attiny25/45/85 a nd will always read as zero. 7.1 idle mode when the sm1..0 bits are written to 00, the sleep i nstruction makes the mcu enter idle mode, stopping the cpu but allowing analog comparator, ad c, timer/counter, watchdog, and the interrupt system to continue operating. this sleep mode basically halts clk cpu and clk flash , while allowing the other clocks to run. idle mode enables the mcu to wake up from external triggered interrupts as well as internal ones like the timer overflow. if wake-up from the a nalog comparator interrupt is not required, the analog comparator can be powered down by settin g the acd bit in the analog comparator control and status register ? acsr. this will reduc e power consumption in idle mode. if the adc is enabled, a conversion starts automatically w hen this mode is entered. 7.2 adc noise reduction mode when the sm1..0 bits are written to 01, the sleep i nstruction makes the mcu enter adc noise reduction mode, stopping the cpu but allowing the a dc, the external interrupts, and the watchdog to continue operating (if enabled). this s leep mode halts clk i/o , clk cpu , and clk flash , while allowing the other clocks to run. this improves the noise environment for the adc, en abling higher resolution measurements. if the adc is enabled, a conversion starts automatical ly when this mode is entered. apart form the adc conversion complete interrupt, only an external reset, a watchdog reset, a brown-out reset, an spm/eeprom ready interrupt, an external l evel interrupt on int0 or a pin change interrupt can wake up the mcu from adc noise reduct ion mode. 7.3 power-down mode when the sm1..0 bits are written to 10, the sleep i nstruction makes the mcu enter power- down mode. in this mode, the oscillator is stopped, while the external interrupts, and the watch- dog continue operating (if enabled). only an extern al reset, a watchdog reset, a brown-out reset, an external level interrupt on int0, or a pi n change interrupt can wake up the mcu. this sleep mode halts all generated clocks, allowing ope ration of asynchronous modules only.
33 7598d?avr?02/07 attiny25/45/85 auto note that if a level triggered interrupt is used fo r wake-up from power-down mode, the changed level must be held for some time to wake up the mcu . refer to ?external interrupts? on page 58 for details . . note: 1. for int0, only level interrupt. 7.4 power reduction register the power reduction register, prr, provides a metho d to stop the clock to individualperipher- als to reduce power consumption. the current state of the peripheral is frozenand the i/o registers can not be read or written. resources use d by the peripheral when stopping the clock will remain occupied, hence the peripheral should i n most cases be disabled before stopping the clock. waking up a module, which is done by clearin g the bit in prr, puts the module in the same state as before shutdown. module shutdown can be used in idle mode and active mode to significantly reduce the overall power consumption. in all other sleep modes, the cl ock is already stopped. ? bits 7, 6, 5, 4- res: reserved bits these bits are reserved bits in the attiny25/45/85 and will always read as zero. ? bit 3- prtim1: power reduction timer/counter1 writing a logic one to this bit shuts down the time r/counter1 module. when the timer/counter1 is enabled, operation will continue like before the shutdown. ? bit 2- prtim0: power reduction timer/counter0 writing a logic one to this bit shuts down the time r/counter0 module. when the timer/counter0 is enabled, operation will continue like before the shutdown. ? bit 1 - prusi: power reduction usi writing a logic one to this bit shuts down the usi by stopping the clock to the module. when waking up the usi again, the usi should be re initi alized to ensure proper operation. ? bit 0 - pradc: power reduction adc table 7-2. active clock domains and wake-up sources in the dif ferent sleep modes active clock domains oscillators wake-up sources sleep mode clk cpu clk flash clk io clk adc clk pck main clock source enabled int0 and pin change spm/ eeprom ready adc other i/o watchdog interrupt idle x x x x x x x x x adc noise reduction x x x (1) x x x power-down x (1) x bit 7 6 5 4 3 2 1 0 ? - - - prtim1 prtim0 prusi pradc prr read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
34 7598d?avr?02/07 attiny25/45/85 auto writing a logic one to this bit shuts down the adc. the adc must be disabled before shut down. the analog comparator cannot use the adc input mux when the adc is shut down. 7.5 minimizing power consumption there are several issues to consider when trying to minimize the power consumption in an avr controlled system. in general, sleep modes should b e used as much as possible, and the sleep mode should be selected so that as few as possible of the device?s functions are operating. all functions not needed should be disabled. in particu lar, the following modules may need special consideration when trying to achieve the lowest pos sible power consumption. 7.5.1 analog to digital converter if enabled, the adc will be enabled in all sleep mo des. to save power, the adc should be dis- abled before entering any sleep mode. when the adc is turned off and on again, the next conversion will be an extended conversion. refer to ?analog to digital converter? on page 117 for details on adc operation. 7.5.2 analog comparator when entering idle mode, the analog comparator shou ld be disabled if not used. when entering adc noise reduction mode, the analog comparator sho uld be disabled. in the other sleep modes, the analog comparator is automatically disab led. however, if the analog comparator is set up to use the internal voltage reference as inp ut, the analog comparator should be dis- abled in all sleep modes. otherwise, the internal v oltage reference will be enabled, independent of sleep mode. refer to ?analog comparator? on page 114 for details on how to configure the analog comparator. 7.5.3 brown-out detector if the brown-out detector is not needed in the appl ication, this module should be turned off. if the brown-out detector is enabled by the bodlevel fuses , it will be enabled in all sleep modes, and hence, always consume power. in the deeper slee p modes, this will contribute significantly to the total current consumption. refer to ?brown-out detection? on page 38 for details on how to configure the brown-out detector. 7.5.4 internal voltage reference the internal voltage reference will be enabled when needed by the brown-out detection, the analog comparator or the adc. if these modules are disabled as described in the sections above, the internal voltage reference will be disab led and it will not be consuming power. when turned on again, the user must allow the reference to start up before the output is used. if the reference is kept on in sleep mode, the output can be used immediately. refer to ?internal volt- age reference? on page 40 for details on the start-up time. 7.5.5 watchdog timer if the watchdog timer is not needed in the applicat ion, this module should be turned off. if the watchdog timer is enabled, it will be enabled in al l sleep modes, and hence, always consume power. in the deeper sleep modes, this will contrib ute significantly to the total current consump- tion. refer to ?watchdog timer? on page 40 for details on how to configure the watchdog timer . 7.5.6 port pins when entering a sleep mode, all port pins should be configured to use minimum power. the most important thing is then to ensure that no pins drive resistive loads. in sleep modes where
35 7598d?avr?02/07 attiny25/45/85 auto both the i/o clock (clk i/o ) and the adc clock (clk adc ) are stopped, the input buffers of the device will be disabled. this ensures that no power is con sumed by the input logic when not needed. in some cases, the input logic is needed for detecting wake-up conditions, and it will then be enabled. refer to the section ?digital input enable and sleep modes? on page 51 for details on which pins are enabled. if the input buffer is enab led and the input signal is left floating or has an analog signal level close to v cc /2, the input buffer will use excessive power. for analog input pins, the digital input buffer sho uld be disabled at all times. an analog signal level close to v cc /2 on an input pin can cause significant current ev en in active mode. digital input buffers can be disabled by writing to the dig ital input disable register (didr0). refer to ?digital input disable register 0 ? didr0? on page 117 for details. 8. system control and reset 8.0.1 resetting the avr during reset, all i/o registers are set to their in itial values, and the program starts execution from the reset vector. the instruction placed at th e reset vector must be a rjmp ? relative jump ? instruction to the reset handling routine. i f the program never enables an interrupt source, the interrupt vectors are not used, and reg ular program code can be placed at these locations. the circuit diagram in figure 8-1 shows the reset logic. table 8-1 defines the electrical parameters of the reset circuitry. the i/o ports of the avr are immediately reset to t heir initial state when a reset source goes active. this does not require any clock source to b e running. after all reset sources have gone inactive, a delay counter is invoked, stretching the internal reset. this allows the power to reach a stable leve l before normal operation starts. the time-out period of the delay counter is defined by the user through the sut and cksel fuses. the dif- ferent selections for the delay period are presente d in ?clock sources? on page 23 . 8.0.2 reset sources the attiny25/45/85 has four sources of reset: ? power-on reset. the mcu is reset when the supply v oltage is below the power-on reset threshold (v pot ). ? external reset. the mcu is reset when a low level is present on the reset pin for longer than the minimum pulse length. ? watchdog reset. the mcu is reset when the watchdog timer period expires and the watchdog is enabled. ? brown-out reset. the mcu is reset when the supply voltage v cc is below the brown-out reset threshold (v bot ) and the brown-out detector is enabled.
36 7598d?avr?02/07 attiny25/45/85 auto figure 8-1. reset logic 8.0.3 power-on reset a power-on reset (por) pulse is generated by an on- chip detection circuit. the detection level is defined in table 8-1 . the por is activated whenever v cc is below the detection level. the por circuit can be used to trigger the start-up res et, as well as to detect a failure in supply voltage. a power-on reset (por) circuit ensures that the dev ice is reset from power-on. reaching the power-on reset threshold voltage invokes the delay counter, which determines how long the device is kept in reset after v cc rise. the reset signal is activated again, without any delay, when v cc decreases below the detection level. figure 8-2. mcu start-up, reset tied to v cc mcu status register (mcusr) brown-out reset circuit bodlevel [1..0] delay counters cksel[1:0] ck timeout wdrf borf extrf porf dat a bus clock generator spike filter pull-up resistor wat chdog oscillator sut[1:0] pow er-on reset circuit v reset time-out internal reset t tout v pot v rst cc
37 7598d?avr?02/07 attiny25/45/85 auto figure 8-3. mcu start-up, reset extended externally table 8-1. power on reset specifications note: 1. the power-on reset will not work unless the supply voltage has been below v por . 8.0.4 external reset an external reset is generated by a low level on th e reset pin if enabled. reset pulses longer than the minimum pulse width (see table 8-1 ) will generate a reset, even if the clock is not r un- ning. shorter pulses are not guaranteed to generate a reset. when the applied signal reaches the reset threshold voltage ? v rst ? on its positive edge, the delay counter starts t he mcu after the time-out period ? t tout ? has expired. figure 8-4. external reset during operation symbol parameter min typ max units v pot power-on reset threshold voltage (rising) 1.0 1.4 v power-on reset threshold voltage (falling) (1) 0.9 1.3 v v por vdd start voltage to ensure internal power-on reset signal -0.1 vss 0.4 v v ddrr vdd rise rate to ensure 0.01 v/ms v rst reset pin threshold voltage 0.1 v cc 0.9v cc v t rst minimum pulse width on reset pin 2.5 s cc
38 7598d?avr?02/07 attiny25/45/85 auto 8.0.5 brown-out detection attiny25/45/85 has an on-chip brown-out detection ( bod) circuit for monitoring the v cc level during operation by comparing it to a fixed trigger level. the trigger level for the bod can be selected by the bodlevel fuses. the trigger level h as a hysteresis to ensure spike free brown-out detection. the hysteresis on the detectio n level should be interpreted as v bot+ = v bot + v hyst /2 and v bot- = v bot - v hyst /2. note: 1. v bot may be below nominal minimum operating voltage for some devices. for devices where this is the case, the device is tested down to v cc = v bot during the production test. this guar- antees that a brown-out reset will occur before v cc drops to a voltage where correct operation of the microcontroller is no longer guara nteed. 2. centered value, not tested. notes: 1. this is the limit to which vdd can be lower ed without losing ram data when the bod is enabled, and v cc decreases to a value below the trigger level (v bot- in figure 8-5 ), the brown-out reset is immediately activated. wh en v cc increases above the trigger level (v bot+ in figure 8-5 ), the delay counter starts the mcu after the time- out period t tout has expired. the bod circuit will only detect a drop in v cc if the voltage stays below the trigger level for longer than t bod given in table 8-1 . table 8-2. bodlevel fuse coding (1) bodlevel [2..0] fuses min v bot typ v bot max v bot units 111 bod disabled 110 1.7 1.8 2.0 v 101 2.5 2.7 2.9 100 4.0 4.3 4.6 011 2.3 (2) 010 2.2 (2) 001 1.9 (2) 000 2.0 (2) table 8-3. brown-out characteristics symbol parameter min typ max units v ram ram retention voltage (1) 50 mv v hyst brown-out detector hysteresis 50 mv t bod min pulse width on brown-out reset 2 s
39 7598d?avr?02/07 attiny25/45/85 auto figure 8-5. brown-out reset during operation 8.0.6 watchdog reset when the watchdog times out, it will generate a sho rt reset pulse of one ck cycle duration. on the falling edge of this pulse, the delay timer sta rts counting the time-out period t tout . refer to page 40 for details on operation of the watchdog timer. figure 8-6. watchdog reset during operation 8.0.7 mcu status register ? mcusr the mcu status register provides information on whi ch reset source caused an mcu reset. ? bits 7..4 ? res: reserved bits these bits are reserved bits in the attiny25/45/85 and will always read as zero. ? bit 3 ? wdrf: watchdog reset flag this bit is set if a watchdog reset occurs. the bit is reset by a power-on reset, or by writing a logic zero to the flag. ? bit 2 ? borf: brown-out reset flag this bit is set if a brown-out reset occurs. the bi t is reset by a power-on reset, or by writing a logic zero to the flag. v cc reset time-out internal reset v bot- v bot+ t tout ck cc bit 7 6 5 4 3 2 1 0 ? ? ? ? wdrf borf extrf porf mcusr read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 see bit description
40 7598d?avr?02/07 attiny25/45/85 auto ? bit 1 ? extrf: external reset flag this bit is set if an external reset occurs. the bi t is reset by a power-on reset, or by writing a logic zero to the flag. ? bit 0 ? porf: power-on reset flag this bit is set if a power-on reset occurs. the bit is reset only by writing a logic zero to the flag. to make use of the reset flags to identify a reset condition, the user should read and then reset the mcusr as early as possible in the program. if t he register is cleared before another reset occurs, the source of the reset can be found by exa mining the reset flags. 8.1 internal voltage reference attiny25/45/85 features an internal bandgap referen ce. this reference is used for brown-out detection, and it can be used as an input to the an alog comparator or the adc. 8.1.1 voltage reference enable signals and start-up time the voltage reference has a start-up time that may influence the way it should be used. the start-up time is given in table 8-4 . to save power, the reference is not always turned on. the reference is on during the following situations: 1. when the bod is enabled (by programming the bodle vel [2..0] fuse). 2. when the bandgap reference is connected to the an alog comparator (by setting the acbg bit in acsr). 3. when the adc is enabled. thus, when the bod is not enabled, after setting th e acbg bit or enabling the adc, the user must always allow the reference to start up before the output from the analog comparator or adc is used. to reduce power consumption in power-d own mode, the user can avoid the three conditions above to ensure that the reference is tu rned off before entering power-down mode. 8.2 watchdog timer the watchdog timer is clocked from an on-chip oscil lator which runs at 128 khz. by controlling the watchdog timer prescaler, the watchdog reset in terval can be adjusted as shown in table 8-7 on page 43 . the wdr ? watchdog reset ? instruction resets the watchdog timer. the watchdog timer is also reset when it is disabled an d when a chip reset occurs. ten different clock cycle periods can be selected to determine th e reset period. if the reset period expires without another watchdog reset, the attiny25/45/85 resets and executes from the reset vec- tor. for timing details on the watchdog reset, refe r to table 8-7 on page 43 . the wathdog timer can also be configured to generat e an interrupt instead of a reset. this can be very helpful when using the watchdog to wake-up from power-down. table 8-4. internal voltage reference characteristics symbol parameter condition min typ max units v bg bandgap reference voltage v cc = 1.1v / 2.7v, t a = 25c 1.0 1.1 1.2 v t bg bandgap reference start-up time v cc = 2.7v, t a = 25c 40 70 s i bg bandgap reference current consumption v cc = 2.7v, t a = 25c 15 a
41 7598d?avr?02/07 attiny25/45/85 auto to prevent unintentional disabling of the watchdog or unintentional change of time-out period, two different safety levels are selected by the fus e wdton as shown in table 8-5. refer to ?timed sequences for changing the configuration of the watchdog timer? on page 44 for details. figure 8-7. watchdog timer 8.2.1 watchdog timer control register ? wdtcr ? bit 7 ? wdif: watchdog timeout interrupt flag this bit is set when a time-out occurs in the watch dog timer and the watchdog timer is config- ured for interrupt. wdif is cleared by hardware whe n executing the corresponding interrupt handling vector. alternatively, wdif is cleared by writing a logic one to the flag. when the i-bit in sreg and wdie are set, the watchdog time-out interr upt is executed. ? bit 6 ? wdie: watchdog timeout interrupt enable when this bit is written to one, wde is cleared, an d the i-bit in the status register is set, the watchdog time-out interrupt is enabled. in this mod e the corresponding interrupt is executed instead of a reset if a timeout in the watchdog tim er occurs. if wde is set, wdie is automatically cleared by har dware when a time-out occurs. this is useful for keeping the watchdog reset security while using the interrupt. after the wdie bit is cleared, table 8-5. wdt configuration as a function of the fuse setting s of wdton wdton safety level wdt initial state how to disable the wdt how to change time- out unprogrammed 1 disabled timed sequence no limitations programmed 2 enabled always enabled timed sequence osc/2kosc/4k osc/8k osc/16kosc/32k osc/64k osc/128kosc/256k osc/512k osc/1024k mcu reset wat chdog prescaler 128 khz oscillator wat chdog reset wdp0wdp1 wdp2 wdp3 wde bit 7 6 5 4 3 2 1 0 wdif wdie wdp3 wdce wde wdp2 wdp1 wdp0 wdtcr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 x 0 0 0
42 7598d?avr?02/07 attiny25/45/85 auto the next time-out will generate a reset. to avoid t he watchdog reset, wdie must be set after each interrupt. ? bit 4 ? wdce: watchdog change enable this bit must be set when the wde bit is written to logic zero. otherwise, the watchdog will not be disabled. once written to one, hardware will cle ar this bit after four clock cycles. refer to the description of the wde bit for a watchdog disable p rocedure. this bit must also be set when changing the prescaler bits. see ?timed sequences for changing the configuration of the watchdog timer? on page 44. ? bit 3 ? wde: watchdog enable when the wde is written to logic one, the watchdog timer is enabled, and if the wde is written to logic zero, the watchdog timer function is disab led. wde can only be cleared if the wdce bit has logic level one. to disable an enabled watchdog timer, the following procedure must be followed: 1. in the same operation, write a logic one to wdce and wde. a logic one must be writ- ten to wde even though it is set to one before the disable operation starts. 2. within the next four clock cycles, write a logic 0 to wde. this disables the watchdog. in safety level 2, it is not possible to disable th e watchdog timer, even with the algorithm described above. see ?timed sequences for changing the configuration of the watchdog timer? on page 44. in safety level 1, wde is overridden by wdrf in mcu sr. see ?mcu status register ? mcusr? on page 39 for description of wdrf. this means that wde is al ways set when wdrf is set. to clear wde, wdrf must be cleared before d isabling the watchdog with the procedure described above. this feature ensures multiple rese ts during conditions causing failure, and a safe start-up after the failure. note: if the watchdog timer is not going to be used in the application, it is important to go through a watchdog disable procedure in the initialization of the device. if the watchdog is accidentally enabled, for example by a runaway pointer or brown- out condition, the device will be reset, which in turn will lead to a new watchdog reset. to avoid this situation, the application software should always clear the wdrf flag and the wde control bit in the initialization routine. ? bits 5, 2..0 ? wdp3..0: watchdog timer prescaler 3 , 2, 1, and 0 table 8-6. watchdog timer configuration wde wdie watchdog timer state action on time-out 0 0 stopped none 0 1 running interrupt 1 0 running reset 1 1 running interrupt
43 7598d?avr?02/07 attiny25/45/85 auto the wdp3..0 bits determine the watchdog timer presc aling when the watchdog timer is enabled. the different prescaling values and their corresponding timeout periods are shown in table 8-7 . table 8-7. watchdog timer prescale select wdp3 wdp2 wdp1 wdp0 number of wdt oscillator cycles typical time-out at v cc = 5.0v 0 0 0 0 2k cycles 16 ms 0 0 0 1 4k cycles 32 ms 0 0 1 0 8k cycles 64 ms 0 0 1 1 16k cycles 0.125 s 0 1 0 0 32k cycles 0.25 s 0 1 0 1 64k cycles 0.5 s 0 1 1 0 128k cycles 1.0 s 0 1 1 1 256k cycles 2.0 s 1 0 0 0 512k cycles 4.0 s 1 0 0 1 1024k cycles 8.0 s 1 0 1 0 reserved 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1
44 7598d?avr?02/07 attiny25/45/85 auto the following code example shows one assembly and o ne c function for turning off the wdt. the example assumes that interrupts are controlled (e.g., by disabling interrupts globally) so that no interrupts will occur during execution of these functions. note: 1. the example code assumes that the part speci fic header file is included. 8.3 timed sequences for changing the configuration o f the watchdog timer the sequence for changing configuration differs sli ghtly between the two safety levels. separate procedures are described for each level. 8.3.1 safety level 1 in this mode, the watchdog timer is initially disab led, but can be enabled by writing the wde bit to one without any restriction. a timed sequence is needed when disabling an enabled watch- dog timer. to disable an enabled watchdog timer, th e following procedure must be followed: 1. in the same operation, write a logic one to wdce and wde. a logic one must be writ- ten to wde regardless of the previous value of the wde bit. 2. within the next four clock cycles, in the same op eration, write the wde and wdp bits as desired, but with the wdce bit cleared. assembly code example (1) wdt_off: wdr ; clear wdrf in mcusr ldi r16, (0< 45 7598d?avr?02/07 attiny25/45/85 auto 8.3.2 safety level 2 in this mode, the watchdog timer is always enabled, and the wde bit will always read as one. a timed sequence is needed when changing the watchdog time-out period. to change the watchdog time-out, the following procedure must be followed: 1. in the same operation, write a logical one to wdc e and wde. even though the wde always is set, the wde must be written to one to st art the timed sequence. 2. within the next four clock cycles, in the same op eration, write the wdp bits as desired, but with the wdce bit cleared. the value written to the wde bit is irrelevant. 9. interrupts this section describes the specifics of the interru pt handling as performed in attiny25/45/85. for a general explanation of the avr interrupt hand ling, refer to ?reset and interrupt handling? on page 11 . 9.1 interrupt vectors in attiny25/45/85 if the program never enables an interrupt source, t he interrupt vectors are not used, and regular program code can be placed at these locations. the most typical and general program setup for the reset and interrupt vector addresses in attiny2 5/45/85 is: address labels code comments 0x0000 rjmp reset ; reset handler 0x0001 rjmp ext_int0 ; irq0 handler 0x0002 rjmp pcint0 ; pcint0 handler table 9-1. reset and interrupt vectors vector no. program address source interrupt definition 1 0x0000 reset external pin, power-on reset, brown-out reset, watchdog reset 2 0x0001 int0 external interrupt request 0 3 0x0002 pcint0 pin change interrupt request 0 4 0x0003 tim1_compa timer/counter1 compare match a 5 0x0004 tim1_ovf timer/counter1 overflow 6 0x0005 tim0_ovf timer/counter0 overflow 7 0x0006 ee_rdy eeprom ready 8 0x0007 ana_comp analog comparator 9 0x0008 adc adc conversion complete 10 0x0009 tim1_compb timer/counter1 compare match b 11 0x000a tim0_compa timer/counter0 compare match a 12 0x000b tim0_compb timer/counter0 compare match b 13 0x000c wdt watchdog time-out 14 0x000d usi_start usi start 15 0x000e usi_ovf usi overflow
46 7598d?avr?02/07 attiny25/45/85 auto 0x0003 rjmp tim1_compa ; timer1 comparea handler 0x0004 rjmp tim1_ovf ; timer1 overflow handler 0x0005 rjmp tim0_ovf ; timer0 overflow handler 0x0006 rjmp ee_rdy ; eeprom ready handler 0x0007 rjmp ana_comp ; analog comparator handler 0x0008 rjmp adc ; adc conversion handler 0x0009 rjmp tim1_compb ; timer1 compareb handler 0x000a rjmp tim0_compa ; 0x000b rjmp tim0_compb ; 0x000c rjmp wdt ; 0x000d rjmp usi_start ; 0x000e rjmp usi_ovf ; 0x000f reset: ldi r16, low(ramend); main program start 0x0010 ldi r17, high(ramend); tiny85 has also sph 0x0011 out spl, r16 ; set stack pointer to top of ram 0x0012 out sph, r17 ; tiny85 has also sph 0x0013 sei ; enable interrupts 0x0014 xxx ... ... ... ... 10. i/o ports 10.1 introduction all avr ports have true read-modify-write functiona lity when used as general digital i/o ports. this means that the direction of one port pin can b e changed without unintentionally changing the direction of any other pin with the sbi and cbi instructions. the same applies when chang- ing drive value (if configured as output) or enabli ng/disabling of pull-up resistors (if configured as input). each output buffer has symmetrical drive ch aracteristics with both high sink and source capability. the pin driver is strong enough to driv e led displays directly. all port pins have indi- vidually selectable pull-up resistors with a supply -voltage invariant resistance. all i/o pins have protection diodes to both v cc and ground as indicated in figure 10-1 . refer to ?electrical char- acteristics? on page 156 for a complete list of parameters.
47 7598d?avr?02/07 attiny25/45/85 auto figure 10-1. i/o pin equivalent schematic all registers and bit references in this section ar e written in general form. a lower case ?x? repre- sents the numbering letter for the port, and a lowe r case ?n? represents the bit number. however, when using the register or bit defines in a program , the precise form must be used. for example, portb3 for bit no. 3 in port b, here documented gen erally as portxn. the physical i/o regis- ters and bit locations are listed in ?register description for i/o-ports? on page 57 . three i/o memory address locations are allocated fo r each port, one each for the data register ? portx, data direction register ? ddrx, and the po rt input pins ? pinx. the port input pins i/o location is read only, while the data register and the data direction register are read/write. however, writing a logic one to a bit in the pinx r egister, will result in a toggle in the correspond- ing bit in the data register. in addition, the pull -up disable ? pud bit in mcucr disables the pull-up function for all pins in all ports when set . using the i/o port as general digital i/o is descri bed in ?ports as general digital i/o? on page 47 . most port pins are multiplexed with alternate fun ctions for the peripheral features on the device. how each alternate function interferes with the port pin is described in ?alternate port functions? on page 52 . refer to the individual module sections for a ful l description of the alter- nate functions. note that enabling the alternate function of some o f the port pins does not affect the use of the other pins in the port as general digital i/o. 10.2 ports as general digital i/o the ports are bi-directional i/o ports with optiona l internal pull-ups. figure 10-2 shows a func- tional description of one i/o-port pin, here generi cally called pxn. c pin logic r pu see figure "general digital i/o" for details pxn
48 7598d?avr?02/07 attiny25/45/85 auto figure 10-2. general digital i/o (1) note: 1. wrx, wpx, wdx, rrx, rpx, and rdx are common to all pins within the same port. clk i/o , sleep, and pud are common to all ports. 10.2.1 configuring the pin each port pin consists of three register bits: ddxn , portxn, and pinxn. as shown in ?register description for i/o-ports? on page 57 , the ddxn bits are accessed at the ddrx i/o addres s, the portxn bits at the portx i/o address, and the pinxn bits at the pinx i/o address. the ddxn bit in the ddrx register selects the direc tion of this pin. if ddxn is written logic one, pxn is configured as an output pin. if ddxn is writ ten logic zero, pxn is configured as an input pin. if portxn is written logic one when the pin is conf igured as an input pin, the pull-up resistor is activated. to switch the pull-up resistor off, port xn has to be written logic zero or the pin has to be configured as an output pin. the port pins are t ri-stated when reset condition becomes active, even if no clocks are running. if portxn is written logic one when the pin is conf igured as an output pin, the port pin is driven high (one). if portxn is written logic zero when th e pin is configured as an output pin, the port pin is driven low (zero). 10.2.2 toggling the pin writing a logic one to pinxn toggles the value of p ortxn, independent on the value of ddrxn. note that the sbi instruction can be used to toggle one single bit in a port. clk rpx rrx rdx wdx pud synchronizer wdx: write ddrx wrx: write portx rrx: read portx register rpx: read portx pin pud: pullup disable clk i/o : i/o clock rdx: read ddrx dl qq reset reset q q d q q d clr portxn q q d clr ddxn pinxn dat a bus sleep sleep: sleep control pxn i/o wpx 0 1 wrx wpx: write pinx register
49 7598d?avr?02/07 attiny25/45/85 auto 10.2.3 switching between input and output when switching between tri-state ({ddxn, portxn} = 0b00) and output high ({ddxn, portxn} = 0b11), an intermediate state with either pull-up enabled {ddxn, portxn} = 0b01) or output low ({ddxn, portxn} = 0b10) must occur. normally, t he pull-up enabled state is fully accept- able, as a high-impedant environment will not notic e the difference between a strong high driver and a pull-up. if this is not the case, the pud bit in the mcucr register can be set to disable all pull-ups in all ports. switching between input with pull-up and output low generates the same problem. the user must use either the tri-state ({ddxn, portxn} = 0b0 0) or the output high state ({ddxn, portxn} = 0b10) as an intermediate step. table 10-1 summarizes the control signals for the pin value. 10.2.4 reading the pin value independent of the setting of data direction bit dd xn, the port pin can be read through the pinxn register bit. as shown in figure 10-2 , the pinxn register bit and the preceding latch co n- stitute a synchronizer. this is needed to avoid met astability if the physical pin changes value near the edge of the internal clock, but it also in troduces a delay. figure 10-3 shows a timing dia- gram of the synchronization when reading an externa lly applied pin value. the maximum and minimum propagation delays are denoted t pd,max and t pd,min respectively. figure 10-3. synchronization when reading an externally applied pin value table 10-1. port pin configurations ddxn portxn pud (in mcucr) i/o pull-up comment 0 0 x input no tri-state (hi-z) 0 1 0 input yes pxn will source current if ext. pulled l ow. 0 1 1 input no tri-state (hi-z) 1 0 x output no output low (sink) 1 1 x output no output high (source) xxx in r17, pinx 0x00 0xff instructions sync latch pinxn r17 xxx system clk t pd, max t pd, min
50 7598d?avr?02/07 attiny25/45/85 auto consider the clock period starting shortly after th e first falling edge of the system clock. the latch is closed when the clock is low, and goes transpare nt when the clock is high, as indicated by the shaded region of the ?sync latch? signal. the signa l value is latched when the system clock goes low. it is clocked into the pinxn register at the succeeding positive clock edge. as indi- cated by the two arrows tpd,max and tpd,min, a sing le signal transition on the pin will be delayed between ? and 1? system clock period depending upon the time of assertion. when reading back a software assigned pin value, a nop instruction must be inserted as indi- cated in figure 10-4 . the out instruction sets the ?sync latch? signal at the positive edge of the clock. in this case, the delay tpd through the synchronizer is one system clock period. figure 10-4. synchronization when reading a software assigned pi n value the following code example shows how to set port b pins 0 and 1 high, 2 and 3 low, and define the port pins from 4 to 5 as input with a pull-up a ssigned to port pin 4. the resulting pin values are read back again, but as previously discussed, a nop instruction is included to be able to read back the value recently assigned to some of the pin s. out portx, r16 nop in r17, pinx 0xff 0x00 0xff system clk r16 instructions sync latch pinxn r17 t pd
51 7598d?avr?02/07 attiny25/45/85 auto note: 1. for the assembly program, two temporary regi sters are used to minimize the time from pull- ups are set on pins 0, 1 and 4, until the direction bits are correctly set, defining bit 2 and 3 as low and redefining bits 0 and 1 as strong high driv ers. 10.2.5 digital input enable and sleep modes as shown in figure 10-2 , the digital input signal can be clamped to ground at the input of the schmitt-trigger. the signal denoted sleep in the fi gure, is set by the mcu sleep controller in power-down mode, power-save mode, and standby mode to avoid high power consumption if some input signals are left floating, or have an an alog signal level close to v cc /2. sleep is overridden for port pins enabled as extern al interrupt pins. if the external interrupt request is not enabled, sleep is active also for th ese pins. sleep is also overridden by various other alternate functions as described in ?alternate port functions? on page 52 . if a logic high level (?one?) is present on an asyn chronous external interrupt pin configured as ?interrupt on rising edge, falling edge, or any log ic change on pin? while the external interrupt is not enabled, the corresponding external interrupt flag will be set when resuming from the above mentioned sleep mode, as the clamping in thes e sleep mode produces the requested logic change. assembly code example (1) ... ; define pull-ups and set outputs high ; define directions for port pins ldi r16,(1< 52 7598d?avr?02/07 attiny25/45/85 auto 10.2.6 unconnected pins if some pins are unused, it is recommended to ensur e that these pins have a defined level. even though most of the digital inputs are disabled in t he deep sleep modes as described above, float- ing inputs should be avoided to reduce current cons umption in all other modes where the digital inputs are enabled (reset, active mode and idle mod e). the simplest method to ensure a defined level of an unused pin, is to enable the internal pull-up. in this case, the pull-up will be disabled during r eset. if low power consumption during reset is important, it is recommended to use an external pul l-up or pulldown. connecting unused pins directly to v cc or gnd is not recommended, since this may cause ex cessive currents if the pin is accidentally configured as an output. 10.3 alternate port functions most port pins have alternate functions in addition to being general digital i/os. figure 10-5 shows how the port pin control signals from the sim plified figure 10-2 can be overridden by alternate functions. the overriding signals may not be present in all port pins, but the figure serves as a generic description applicable to all p ort pins in the avr microcontroller family. figure 10-5. alternate port functions (1) clk rpx rrx wrx rdx wdx pud synchronizer wdx: write ddrx wrx: write portx rrx: read portx register rpx: read portx pin pud: pullup disable clk i/o : i/o clock rdx: read ddrx dl qq set clr 0 1 0 1 0 1 dixnaioxn dieoexn pvovxn pvoexn ddovxn ddoexn puoexn puovxn puoexn: pxn pull-up override enable puovxn: pxn pull-up override val ue ddoexn: pxn da ta direction override enable ddovxn: pxn da ta direction o verride v alue pvoexn: pxn port val ue override enable pvovxn: pxn port val ue override val ue dixn: digital input pin n on portx aioxn: analog input/output pin n on portx reset reset q q d clr q q d clr q q d clr pinxn portxn ddxn dat a bus 0 1 dieovxn sleep dieoexn: pxn digital input-enable override enable dieovxn: pxn digital input-enable override val ue sleep: sleep control pxn i/o 0 1 ptoexn ptoexn: pxn, port tog gle override enable wpx: write pinx wpx
53 7598d?avr?02/07 attiny25/45/85 auto note: 1. wrx, wpx, wdx, rrx, rpx, and rdx are common to all pins within the same port. clk i/o , sleep, and pud are common to all ports. all other s ignals are unique for each pin. table 10-2 summarizes the function of the overriding signals. the pin and port indexes from fig- ure 10-5 are not shown in the succeeding tables. the overri ding signals are generated internally in the modules having the alternate function. the following subsections shortly describe the alte rnate functions for each port, and relate the overriding signals to the alternate function. refer to the alternate function description for further details. 10.3.1 mcu control register ? mcucr table 10-2. generic description of overriding signals for alter nate functions signal name full name description puoe pull-up override enable if this signal is set, the pull-up enable is contro lled by the puov signal. if this signal is cleared, the pull-up is e nabled when {ddxn, portxn, pud} = 0b010. puov pull-up override value if puoe is set, the pull-up is enabled/disabled whe n puov is set/cleared, regardless of the setting of the ddxn, portxn, and pud register bits. ddoe data direction override enable if this signal is set, the output driver enable is controlled by the ddov signal. if this signal is cleared, the output driver is enabled by the ddxn register bit. ddov data direction override value if ddoe is set, the output driver is enabled/disabl ed when ddov is set/cleared, regardless of the setting of t he ddxn register bit. pvoe port value override enable if this signal is set and the output driver is enab led, the port value is controlled by the pvov signal. if pvoe is cleared, and the output driver is enabled, the port value is con trolled by the portxn register bit. pvov port value override value if pvoe is set, the port value is set to pvov, rega rdless of the setting of the portxn register bit. ptoe port toggle override enable if ptoe is set, the portxn register bit is inverted . dieoe digital input enable override enable if this bit is set, the digital input enable is con trolled by the dieov signal. if this signal is cleared, the digita l input enable is determined by mcu state (normal mode, sleep mode ). dieov digital input enable override value if dieoe is set, the digital input is enabled/disab led when dieov is set/cleared, regardless of the mcu state ( normal mode, sleep mode). di digital input this is the digital input to alternate functions. i n the figure, the signal is connected to the output of the schmitt-tr igger but before the synchronizer. unless the digital input i s used as a clock source, the module with the alternate functio n will use its own synchronizer. aio analog input/output this is the analog input/output to/from alternate f unctions. the signal is connected directly to the pad, and can be used bi- directionally. bit 7 6 5 4 3 2 1 0 ? pud se sm1 sm0 ? isc01 isc00 mcucr
54 7598d?avr?02/07 attiny25/45/85 auto ? bits 7, 2? res: reserved bits these bits are reserved bits in the attiny25/45/85 and will always read as zero. ? bit 6 ? pud: pull-up disable when this bit is written to one, the pull-ups in th e i/o ports are disabled even if the ddxn and portxn registers are configured to enable the pull- ups ({ddxn, portxn} = 0b01). see ?con- figuring the pin? on page 48 for more details about this feature. 10.3.2 alternate functions of port b the port b pins with alternate function are shown i n table 10-3 . notes: 1. reset pin, debugwire i/o, adc input channel or pin change interrupt. 2. xosc output, divided system clock output, adc inp ut channel, timer/counter1 output compare and pwm output b, or pin change interrupt. 3. xosc input / external clock input, adc input chan nel, timer/counter1 inverted output com- pare and pwm output b, or pin change interrupt. 4. serial clock input, adc input channel, timer/coun ter clock input, usi clock (three-wire mode), usi clock (two-wire mode), external interrup t, or pin change interrupt. 5. serial data input, analog comparator negative inp ut, timer/counter0 output compare and pwm output b, timer/counter1 output compare and pwm output a, usi data output (three- wire mode), or pin change interrupt. 6. serial data output, analog comparator positive in put, timer/counter0 output compare and pwm output a, timer/counter1 inverted output compar e and pwm output a, usi data input (three-wire mode), usi data (two-wire mode), voltag e ref., or pin change interrupt. ? port b, bit 5 - reset /dw/adc0/pcint5 reset : external reset input is active low and enabled by unprogramming (?1?) the rstdisbl fuse. pullup is activated and output driver and dig ital input are deactivated when the pin is used as the reset pin. dw: when the debugwire enable (dwen) fuse is progra mmed and lock bits are unpro- grammed, the debugwire system within the target dev ice is activated. the reset port pin is configured as a wire-and (open-drain) bi-directiona l i/o pin with pull-up enabled and becomes the communication gateway between target and emulat or. adc0: analog to digital converter, channel 0 . pcint5: pin change interrupt source 5. read/write r r/w r/w r/w r/w r r r initial value 0 0 0 0 0 0 0 0 table 10-3. port b pins alternate functions port pin alternate function pb5 reset / dw / adc0 / pcint5 (1) pb4 xtal2 / clko / adc2 / oc1b / pcint4 (2) pb3 xtal1 / adc3 / oc1b / pcint3 (3) pb2 sck / adc1 / t0 / usck / scl / int0 / pcint2 (4) pb1 miso / ain1 / oc0b / oc1a / do / pcint1 (5) pb0 mosi / ain0 / oc0a / oc1a / di / sda / aref / pcint0 (6)
55 7598d?avr?02/07 attiny25/45/85 auto ? port b, bit 4- xtal2/clko/adc2/oc1b/pcint4 xtal2: chip clock oscillator pin 2. used as clock p in for all chip clock sources except internal calibrateble rc oscillator and external clock. when used as a clock pin, the pin can not be used as an i/o pin. when using internal calibratable rc oscillator or external clock as a chip clock sources, pb4 serves as an ordinary i/o pin. clko: the devided system clock can be output on the pin pb4. the divided system clock will be output if the ckout fuse is programmed, regardless of the portb4 and ddb4 settings. it will also be output during reset. adc2: analog to digital converter, channel 2 . oc1b: output compare match output: the pb4 pin can serve as an external output for the timer/counter1 compare match b when configured as a n output (ddb4 set). the oc1b pin is also the output pin for the pwm mode timer function . pcint4: pin change interrupt source 4. ? port b, bit 3 - xtal1/adc3/oc1b /pcint3 xtal1: chip clock oscillator pin 1. used for all ch ip clock sources except internal calibrateble rc oscillator. when used as a clock pin, the pin ca n not be used as an i/o pin. adc3: analog to digital converter, channel 3 . oc1b : inverted output compare match output: the pb3 pin can serve as an external output for the timer/counter1 compare match b when configured as an output (ddb3 set). the oc1b pin is also the inverted output pin for the pwm mode ti mer function. pcint3: pin change interrupt source 3. ? port b, bit 2 - sck/adc1/t0/usck/scl/int0/pcint2 sck: master clock output, slave clock input pin for spi channel. when the spi is enabled as a slave, this pin is configured as an input regardles s of the setting of ddb2. when the spi is enabled as a master, the data direction of this pin is controlled by ddpb2. when the pin is forced by the spi to be an input, the pull-up can s till be controlled by the portb2 bit. adc1: analog to digital converter, channel 1 . t0: timer/counter0 counter source. usck: three-wire mode universal serial interface cl ock. scl: two-wire mode serial clock for usi two-wire mo de. int0: external interrupt source 0. pcint2: pin change interrupt source 2. ? port b, bit 1 - miso/ain1/oc0b/oc1a/do/pcint1 miso: master data input, slave data output pin for spi channel. when the spi is enabled as a master, this pin is configured as an input regardle ss of the setting of ddb1. when the spi is enabled as a slave, the data direction of this pin is controlled by ddb1. when the pin is forced by the spi to be an input, the pull-up can still be controlled by the portb1 bit. ain1: analog comparator negative input. configure t he port pin as input with the internal pull-up switched off to avoid the digital port function fro m interfering with the function of the analog comparator.
56 7598d?avr?02/07 attiny25/45/85 auto oc0b: output compare match output. the pb1 pin can serve as an external output for the timer/counter0 compare match b. the pb1 pin has to be configured as an output (ddb1 set (one)) to serve this function. the oc0b pin is also the output pin for the pwm mode timer function. oc1a: output compare match output: the pb1 pin can serve as an external output for the timer/counter1 compare match b when configured as a n output (ddb1 set). the oc1a pin is also the output pin for the pwm mode timer function . do: three-wire mode universal serial interface data output. three-wire mode data output over- rides portb1 value and it is driven to the port whe n data direction bit ddb1 is set (one). portb1 still enables the pull-up, if the direction is input and portb1 is set (one). pcint1: pin change interrupt source 1. ? port b, bit 0 - mosi/ain0/oc0a/oc1a /di/sda/aref/pcint0 mosi: spi master data output, slave data input for spi channel. when the spi is enabled as a slave, this pin is configured as an input regardles s of the setting of ddb0. when the spi is enabled as a master, the data direction of this pin is controlled by ddb0. when the pin is forced by the spi to be an input, the pull-up can still be controlled by the portb0 bit. ain0: analog comparator positive input. configure t he port pin as input with the internal pull-up switched off to avoid the digital port function fro m interfering with the function of the analog comparator. oc0a: output compare match output. the pb0 pin can serve as an external output for the timer/counter0 compare match a when configured as a n output (ddb0 set (one)). the oc0a pin is also the output pin for the pwm mode timer f unction. oc1a : inverted output compare match output: the pb0 pin can serve as an external output for the timer/counter1 compare match b when configured as an output (ddb0 set). the oc1a pin is also the inverted output pin for the pwm mode ti mer function. sda: two-wire mode serial interface data. aref: external analog reference for adc. pullup and output driver are disabled on pb0 when the pin is used as an external reference or interna l voltage reference with external capacitor at the aref pin. di: data input in usi three-wire mode. usi three-wi re mode does not override normal port functions, so pin must be configure as an input for di function. pcint0: pin change interrupt source 0.
57 7598d?avr?02/07 attiny25/45/85 auto table 10-4 and table 10-5 relate the alternate functions of port b to the ov erriding signals shown in figure 10-5 on page 52 . note: 1. 1 when the fuse is ?0? (programmed). 10.4 register description for i/o-ports table 10-4. overriding signals for alternate functions in pb5.. pb3 signal name pb5/reset/ adc0/pcint5 pb4/adc2/xtal2/ oc1b /pcint4 pb3/adc3/xtal1/ _oc1b /pcint3 puoe rstdisbl (1) ? dwen (1) 0 0 puov 1 0 0 ddoe rstdisbl (1) ? dwen (1) 0 0 ddov debugwire transmit 0 0 pvoe 0 oc1b enable _oc1b enable pvov 0 oc1b _oc1b ptoe 0 0 0 dieoe rstdisbl (1) + (pcint5 ? pcie + adc0d) pcint4 ? pcie + adc2d pcint3 ? pcie + adc3d dieov adc0d adc2d adc3d di pcint5 input pcint4 input pcint3 input aio reset input, adc0 input adc2 input adc3 input table 10-5. overriding signals for alternate functions in pb3.. pb0 signal name pb2/sck/adc1/t0/ usck /scl /int0/pcint2 pb1/miso /do /ain1/ oc1a /oc0b /pcint1 pb0/mosi/di/sda/ain0/ar ef/_oc1a /oc0a / pcint0 puoe 0 0 0 puov 0 0 0 ddoe usi_two_wire 0 usi_two_wire ddov (usi_scl_hold + portb2 ) ? ddb2 0 (sda + portd ) ? ddrb0 pvoe usi_two_wire ? ddrb2 oc0b enable + oc1a enable + usi_three_wire oc0a enable + _oc1a enable + (usi_two_wire ? ddrb0) pvov 0 oc0b + oc1a + do oc0a + _oc1a ptoe usi_ptoe 0 0 dieoe pcint2 ? pcie + adc1d + usisie pcint1 ? pcie + ain1d pcint0 ? pcie + ain0d + usisie dieov adc1d ain1d ain0d di t0/usck/scl/int0/ pcint2 input pcint1 input di/sda/pcint0 input aio adc1 input analog comparator negative input analog comparator positive input
58 7598d?avr?02/07 attiny25/45/85 auto 10.4.1 port b data register ? portb 10.4.2 port b data direction register ? ddrb 10.4.3 port b input pins address ? pinb 11. external interrupts the external interrupts are triggered by the int0 p in or any of the pcint5..0 pins. observe that, if enabled, the interrupts will trigger even if the int0 or pcint5..0 pins are configured as out- puts. this feature provides a way of generating a s oftware interrupt. pin change interrupts pci will trigger if any enabled pcint5..0 pin toggles. the pcmsk register control which pins con- tribute to the pin change interrupts. pin change in terrupts on pcint5..0 are detected asynchronously. this implies that these interrupts can be used for waking the part also from sleep modes other than idle mode. the int0 interrupts can be triggered by a falling o r rising edge or a low level. this is set up as indicated in the specification for the mcu control register ? mcucr. when the int0 interrupt is enabled and is configured as level triggered, the i nterrupt will trigger as long as the pin is held low. note that recognition of falling or rising edg e interrupts on int0 requires the presence of an i/o clock, described in ?clock systems and their distribution? on page 21 . low level interrupt on int0 is detected asynchronously. this implies that this interrupt can be used for waking the part also from sleep modes other than idle mode. the i/o clock is halted in all sleep modes except idle mode. note that if a level triggered interrupt is used fo r wake-up from power-down, the required level must be held long enough for the mcu to complete th e wake-up to trigger the level interrupt. if the level disappears before the end of the start-up time, the mcu will still wake up, but no inter- rupt will be generated. the start-up time is define d by the sut and cksel fuses as described in ?system clock and clock options? on page 21 . 11.0.1 mcu control register ? mcucr the external interrupt control register a contains control bits for interrupt sense control. ? bits 1, 0 ? isc01, isc00: interrupt sense control 0 bit 1 and bit 0 bit 7 6 5 4 3 2 1 0 ? ? portb5 portb4 portb3 portb2 portb1 portb0 portb read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 ? ? ddb5 ddb4 ddb3 ddb2 ddb1 ddb0 ddrb read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 ? ? pinb5 pinb4 pinb3 pinb2 pinb1 pinb0 pinb read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 n/a n/a n/a n/a n/a n/a bit 7 6 5 4 3 2 1 0 ? pud se sm1 sm0 ? isc01 isc00 mcucr read/write r r/w r/w r/w r/w r r/w r/w initial value 0 0 0 0 0 0 0 0
59 7598d?avr?02/07 attiny25/45/85 auto the external interrupt 0 is activated by the extern al pin int0 if the sreg i-flag and the corre- sponding interrupt mask are set. the level and edge s on the external int0 pin that activate the interrupt are defined in table 11-1 . the value on the int0 pin is sampled before detec ting edges. if edge or toggle interrupt is selected, pul ses that last longer than one clock period will generate an interrupt. shorter pulses are not guara nteed to generate an interrupt. if low level interrupt is selected, the low level must be held u ntil the completion of the currently executing instruction to generate an interrupt. 11.0.2 general interrupt mask register ? gimsk ? bits 7, 4..0 ? res: reserved bits these bits are reserved bits in the attiny25/45/85 and will always read as zero. ? bit 6 ? int0: external interrupt request 0 enable when the int0 bit is set (one) and the i-bit in the status register (sreg) is set (one), the exter- nal pin interrupt is enabled. the interrupt sense c ontrol0 bits 1/0 (isc01 and isc00) in the mcu control register (mcucr) define whether the externa l interrupt is activated on rising and/or fall- ing edge of the int0 pin or level sensed. activity on the pin will cause an interrupt request even if int0 is configured as an output. the correspondi ng interrupt of external interrupt request 0 is executed from the int0 interrupt vector. ? bit 5 ? pcie: pin change interrupt enable when the pcie bit is set (one) and the i-bit in the status register (sreg) is set (one) , pin change interrupt is enabled. any change on any enab led pcint5..0 pin will cause an interrupt. the corresponding interrupt of pin chang e interrupt request is executed from the pci interrupt vector . pcint5..0 pins are enabled individually by the pc msk0 register. 11.0.3 general interrupt flag register ? gifr ? bits 7, 4..0 ? res: reserved bits these bits are reserved bits in the attiny25/45/85 and will always read as zero. ? bit 6 ? intf0: external interrupt flag 0 table 11-1. interrupt 0 sense control isc01 isc00 description 0 0 the low level of int0 generates an interrupt requ est. 0 1 any logical change on int0 generates an interrupt request. 1 0 the falling edge of int0 generates an interrupt r equest. 1 1 the rising edge of int0 generates an interrupt re quest. bit 7 6 5 4 3 2 1 0 ? int0 pcie ? ? ? ? ? gimsk read/write r r/w r/w r r r r r initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 ? intf0 pcif ? ? ? ? ? gifr read/write r r/w r/w r r r r r initial value 0 0 0 0 0 0 0 0
60 7598d?avr?02/07 attiny25/45/85 auto when an edge or logic change on the int0 pin trigge rs an interrupt request, intf0 becomes set (one). if the i-bit in sreg and the int0 bit in gim sk are set (one), the mcu will jump to the cor- responding interrupt vector. the flag is cleared wh en the interrupt routine is executed. alternatively, the flag can be cleared by writing a logical one to it. this flag is always cleared when int0 is configured as a level interrupt. ? bit 5 ? pcif: pin change interrupt flag when a logic change on any pcint5..0 pin triggers a n interrupt request, pcif becomes set (one). if the i-bit in sreg and the pcie bit in gim sk are set (one), the mcu will jump to the cor- responding interrupt vector. the flag is cleared wh en the interrupt routine is executed. alternatively, the flag can be cleared by writing a logical one to it. 11.0.4 pin change mask register ? pcmsk ? bits 7, 6 ? res: reserved bits these bits are reserved bits in the attiny25/45/85 and will always read as zero. ? bits 5..0 ? pcint5..0: pin change enable mask 5..0 each pcint5..0 bit selects whether pin change inter rupt is enabled on the corresponding i/o pin. if pcint5..0 is set and the pcie bit in gimsk is set, pin change interrupt is enabled on the corresponding i/o pin. if pcint5..0 is cleared, pin change interrupt on the corresponding i/o pin is disabled. 12. 8-bit timer/counter0 with pwm timer/counter0 is a general purpose 8-bit timer/cou nter module, with two independent output compare units, and with pwm support. it allows accu rate program execution timing (event man- agement) and wave generation. the main features are : ? two independent output compare units ? double buffered output compare registers ? clear timer on compare match (auto reload) ? glitch free, phase correct pulse width modulator (p wm) ? variable pwm period ? frequency generator ? three independent interrupt sources (tov0, ocf0a, a nd ocf0b) 12.1 overview a simplified block diagram of the 8-bit timer/count er is shown in figure 12-1 . for the actual placement of i/o pins, refer to ?pinout attiny25/45/85? on page 2 . cpu accessible i/o registers, including i/o bits and i/o pins, are shown in bold. the device-specific i/o register and bit loca- tions are listed in the ?8-bit timer/counter register description? on page 71 . bit 7 6 5 4 3 2 1 0 ? ? pcint5 pcint4 pcint3 pcint2 pcint1 pcint0 pcmsk read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 1 1 1 1 1 1
61 7598d?avr?02/07 attiny25/45/85 auto figure 12-1. 8-bit timer/counter block diagram 12.1.1 registers the timer/counter (tcnt0) and output compare regist ers (ocr0a and ocr0b) are 8-bit registers. interrupt request (abbreviated to int.re q. in the figure) signals are all visible in the timer interrupt flag register (tifr). all interrupt s are individually masked with the timer inter- rupt mask register (timsk). tifr and timsk are not shown in the figure. the timer/counter can be clocked internally, via th e prescaler, or by an external clock source on the t0 pin. the clock select logic block controls w hich clock source and edge the timer/counter uses to increment (or decrement) its value. the tim er/counter is inactive when no clock source is selected. the output from the clock select logic is referred to as the timer clock (clk t0 ). the double buffered output compare registers (ocr0a and ocr0b) is compared with the timer/counter value at all times. the result of the compare can be used by the waveform gen- erator to generate a pwm or variable frequency outp ut on the output compare pins (oc0a and oc0b). see ?output compare unit? on page 63. for details. the compare match event will also set the compare flag (ocf0a or ocf0b) which can be used to generate an output compare interrupt request. 12.1.2 definitions many register and bit references in this section ar e written in general form. a lower case ?n? replaces the timer/counter number, in this case 0. a lower case ?x? replaces the output com- pare unit, in this case compare unit a or compare u nit b. however, when using the register or bit defines in a program, the precise form must be used, i.e., tcnt0 for accessing timer/counter0 counter value and so on. clock select timer/counter dat a bus ocrna ocrnb = = tcntn wav eform generation wav eform generation ocna ocnb = fixed top val ue control logic = 0 top bottom count clear direction tov n (int.req.) ocna(int.req.) ocnb(int.req.) tccrna tccrnb tn edge detector ( from prescaler ) clk tn
62 7598d?avr?02/07 attiny25/45/85 auto the definitions in table 34 are also used extensively throughout the document. 12.2 timer/counter clock sources the timer/counter can be clocked by an internal or an external clock source. the clock source is selected by the clock select logic which is cont rolled by the clock select (cs02:0) bits located in the timer/counter control register (tccr 0b). for details on clock sources and pres- caler, see ?timer/counter prescaler? on page 77 . 12.3 counter unit the main part of the 8-bit timer/counter is the pro grammable bi-directional counter unit. figure 12-2 shows a block diagram of the counter and its surro undings. figure 12-2. counter unit block diagram signal description (internal signals): count increment or decrement tcnt0 by 1. direction select between increment and decrement. clear clear tcnt0 (set all bits to zero). clk t n timer/counter clock, referred to as clk t0 in the following. top signalize that tcnt0 has reached maximum value. bottom signalize that tcnt0 has reached minimum value (zer o). depending of the mode of operation used, the counte r is cleared, incremented, or decremented at each timer clock (clk t0 ). clk t0 can be generated from an external or internal cloc k source, selected by the clock select bits (cs02:0). when no clock source is selected (cs02:0 = 0) the timer is stopped. however, the tcnt0 value can be a ccessed by the cpu, regardless of whether clk t0 is present or not. a cpu write overrides (has prio rity over) all counter clear or count operations. the counting sequence is determined by the setting of the wgm01 and wgm00 bits located in the timer/counter control register (tccr0a) and the wgm02 bit located in the timer/counter bottom the counter reaches the bottom when it become s 0x00. max the counter reaches its maximum when it becomes 0xff (decimal 255). top the counter reaches the top when it becomes equa l to the highest value in the count sequence. the top value can be a ssigned to be the fixed value 0xff (max) or the value stored in the o cr0a register. the assignment is dependent on the mode of operation. data bus tcntn control logic count tovn (int.req.) clock select top tn edge detector ( from prescaler ) clk tn bottom direction clear
63 7598d?avr?02/07 attiny25/45/85 auto control register b (tccr0b). there are close connec tions between how the counter behaves (counts) and how waveforms are generated on the out put compare output oc0a. for more details about advanced counting sequences and wavef orm generation, see ?modes of opera- tion? on page 65 . the timer/counter overflow flag (tov0) is set accor ding to the mode of operation selected by the wgm01:0 bits. tov0 can be used for generating a cpu interrupt. 12.4 output compare unit the 8-bit comparator continuously compares tcnt0 wi th the output compare registers (ocr0a and ocr0b). whenever tcnt0 equals ocr0a or o cr0b, the comparator signals a match. a match will set the output compare flag (oc f0a or ocf0b) at the next timer clock cycle. if the corresponding interrupt is enabled, t he output compare flag generates an output compare interrupt. the output compare flag is autom atically cleared when the interrupt is exe- cuted. alternatively, the flag can be cleared by so ftware by writing a logical one to its i/o bit location. the waveform generator uses the match sig nal to generate an output according to operating mode set by the wgm02:0 bits and compare output mode (com0x1:0) bits. the max and bottom signals are used by the waveform generat or for handling the special cases of the extreme values in some modes of operation ( see ?modes of operation? on page 65. ). figure 12-3 shows a block diagram of the output compare unit. figure 12-3. output compare unit, block diagram the ocr0x registers are double buffered when using any of the pulse width modulation (pwm) modes. for the normal and clear timer on comp are (ctc) modes of operation, the dou- ble buffering is disabled. the double buffering syn chronizes the update of the ocr0x compare registers to either top or bottom of the counting s equence. the synchronization prevents the occurrence of odd-length, non-symmetrical pwm pulse s, thereby making the output glitch-free. ocfn x (int.req.) = (8-bit comparator ) ocrnx ocnx data bus tcntn wgmn1:0 waveform generator top focn comnx1:0 bottom
64 7598d?avr?02/07 attiny25/45/85 auto the ocr0x register access may seem complex, but thi s is not case. when the double buffering is enabled, the cpu has access to the ocr0x buffer register, and if double buffering is dis- abled the cpu will access the ocr0x directly. 12.4.1 force output compare in non-pwm waveform generation modes, the match out put of the comparator can be forced by writing a one to the force output compare (foc0x) b it. forcing compare match will not set the ocf0x flag or reload/clear the timer, but the oc0x pin will be updated as if a real compare match had occurred (the com0x1:0 bits settings defi ne whether the oc0x pin is set, cleared or toggled). 12.4.2 compare match blocking by tcnt0 write all cpu write operations to the tcnt0 register will block any compare match that occur in the next timer clock cycle, even when the timer is stop ped. this feature allows ocr0x to be initial- ized to the same value as tcnt0 without triggering an interrupt when the timer/counter clock is enabled. 12.4.3 using the output compare unit since writing tcnt0 in any mode of operation will b lock all compare matches for one timer clock cycle, there are risks involved when changing tcnt0 when using the output compare unit, independently of whether the timer/counter is running or not. if the value written to tcnt0 equals the ocr0x value, the compare match will be m issed, resulting in incorrect waveform generation. similarly, do not write the tcnt0 value equal to bottom when the counter is down-counting. the setup of the oc0x should be performed before se tting the data direction register for the port pin to output. the easiest way of setting the oc0x value is to use the force output com- pare (foc0x) strobe bits in normal mode. the oc0x r egisters keep their values even when changing between waveform generation modes. be aware that the com0x1:0 bits are not double buff ered together with the compare value. changing the com0x1:0 bits will take effect immedia tely. 12.5 compare match output unit the compare output mode (com0x1:0) bits have two fu nctions. the waveform generator uses the com0x1:0 bits for defining the output compare ( oc0x) state at the next compare match. also, the com0x1:0 bits control the oc0x pin output source. figure 12-4 shows a simplified schematic of the logic affected by the com0x1:0 bit setting. the i/o registers, i/o bits, and i/o pins in the figure are shown in bold. only the part s of the general i/o port control registers (ddr and port) that are affected by the com0x1:0 bi ts are shown. when referring to the oc0x state, the reference is for the internal oc0x register, not the oc0x pin. if a system reset occur, the oc0x register is reset to ?0?.
65 7598d?avr?02/07 attiny25/45/85 auto figure 12-4. compare match output unit, schematic the general i/o port function is overridden by the output compare (oc0x) from the waveform generator if either of the com0x1:0 bits are set. h owever, the oc0x pin direction (input or out- put) is still controlled by the data direction regi ster (ddr) for the port pin. the data direction register bit for the oc0x pin (ddr_oc0x) must be se t as output before the oc0x value is visi- ble on the pin. the port override function is indep endent of the waveform generation mode. the design of the output compare pin logic allows i nitialization of the oc0x state before the out- put is enabled. note that some com0x1:0 bit setting s are reserved for certain modes of operation. see ?8-bit timer/counter register description? on p age 71. 12.5.1 compare output mode and waveform generation the waveform generator uses the com0x1:0 bits diffe rently in normal, ctc, and pwm modes. for all modes, setting the com0x1:0 = 0 tells the w aveform generator that no action on the oc0x register is to be performed on the next compar e match. for compare output actions in the non-pwm modes refer to table 12-1 on page 72 . for fast pwm mode, refer to table 12-2 on page 72 , and for phase correct pwm refer to table 12-3 on page 72 . a change of the com0x1:0 bits state will have effec t at the first compare match after the bits are written. for non-pwm modes, the action can be force d to have immediate effect by using the foc0x strobe bits. 12.6 modes of operation the mode of operation, i.e., the behavior of the ti mer/counter and the output compare pins, is defined by the combination of the waveform generati on mode (wgm02:0) and compare output mode (com0x1:0) bits. the compare output mode bits do not affect the counting sequence, while the waveform generation mode bits do. the com 0x1:0 bits control whether the pwm out- put generated should be inverted or not (inverted o r non-inverted pwm). for non-pwm modes the com0x1:0 bits control whether the output should be set, cleared, or toggled at a compare match ( see ?compare match output unit? on page 64. ). for detailed timing information refer to figure 12-8 , figure 12-9 , figure 12-10 and figure 12-11 in ?timer/counter timing diagrams? on page 70 . port ddr d q d q ocn pin ocnx d q waveform generator comnx1comnx0 0 1 data bus focn clk i/o
66 7598d?avr?02/07 attiny25/45/85 auto 12.6.1 normal mode the simplest mode of operation is the normal mode ( wgm02:0 = 0). in this mode the counting direction is always up (incrementing), and no count er clear is performed. the counter simply overruns when it passes its maximum 8-bit value (to p = 0xff) and then restarts from the bot- tom (0x00). in normal operation the timer/counter o verflow flag (tov0) will be set in the same timer clock cycle as the tcnt0 becomes zero. the to v0 flag in this case behaves like a ninth bit, except that it is only set, not cleared. howev er, combined with the timer overflow interrupt that automatically clears the tov0 flag, the timer resolution can be increased by software. there are no special cases to consider in the norma l mode, a new counter value can be written anytime. the output compare unit can be used to generate int errupts at some given time. using the out- put compare to generate waveforms in normal mode is not recommended, since this will occupy too much of the cpu time. 12.6.2 clear timer on compare match (ctc) mode in clear timer on compare or ctc mode (wgm02:0 = 2) , the ocr0a register is used to manipulate the counter resolution. in ctc mode the counter is cleared to zero when the counter value (tcnt0) matches the ocr0a. the ocr0a defines the top value for the counter, hence also its resolution. this mode allows greater contr ol of the compare match output frequency. it also simplifies the operation of counting external events. the timing diagram for the ctc mode is shown in figure 12-5 . the counter value (tcnt0) increases until a compare match occurs between tcnt 0 and ocr0a, and then counter (tcnt0) is cleared. figure 12-5. ctc mode, timing diagram an interrupt can be generated each time the counter value reaches the top value by using the ocf0a flag. if the interrupt is enabled, the interr upt handler routine can be used for updating the top value. however, changing top to a value clo se to bottom when the counter is run- ning with none or a low prescaler value must be don e with care since the ctc mode does not have the double buffering feature. if the new value written to ocr0a is lower than the current value of tcnt0, the counter will miss the compare m atch. the counter will then have to count to its maximum value (0xff) and wrap around starting a t 0x00 before the compare match can occur. for generating a waveform output in ctc mode, the o c0a output can be set to toggle its logical level on each compare match by setting the compare output mode bits to toggle mode (com0a1:0 = 1). the oc0a value will not be visible on the port pin unless the data direction for tcntn ocn (toggle) ocnx interrupt flag set 1 4 period 2 3 (comnx1:0 = 1)
67 7598d?avr?02/07 attiny25/45/85 auto the pin is set to output. the waveform generated wi ll have a maximum frequency of f oc0 = f clk_i/o /2 when ocr0a is set to zero (0x00). the waveform f requency is defined by the following equation: the n variable represents the prescale factor (1, 8, 64, 256, or 1024). as for the normal mode of operation, the tov0 flag is set in the same timer clock cycle that the counter counts from max to 0x00. 12.6.3 fast pwm mode the fast pulse width modulation or fast pwm mode (w gm02:0 = 3 or 7) provides a high fre- quency pwm waveform generation option. the fast pwm differs from the other pwm option by its single-slope operation. the counter counts from bottom to top then restarts from bot- tom. top is defined as 0xff when wgm2:0 = 3, and oc r0a when wgm2:0 = 7. in non- inverting compare output mode, the output compare ( oc0x) is cleared on the compare match between tcnt0 and ocr0x, and set at bottom. in inve rting compare output mode, the out- put is set on compare match and cleared at bottom. due to the single-slope operation, the operating frequency of the fast pwm mode can be twi ce as high as the phase correct pwm mode that use dual-slope operation. this high frequ ency makes the fast pwm mode well suited for power regulation, rectification, and dac applic ations. high frequency allows physically small sized external components (coils, capacitors), and therefore reduces total system cost. in fast pwm mode, the counter is incremented until the counter value matches the top value. the counter is then cleared at the following timer clock cycle. the timing diagram for the fast pwm mode is shown in figure 12-6 . the tcnt0 value is in the timing diagram shown as a his- togram for illustrating the single-slope operation. the diagram includes non-inverted and inverted pwm outputs. the small horizontal line mar ks on the tcnt0 slopes represent com- pare matches between ocr0x and tcnt0. figure 12-6. fast pwm mode, timing diagram the timer/counter overflow flag (tov0) is set each time the counter reaches top. if the inter- rupt is enabled, the interrupt handler routine can be used for updating the compare value. f ocnx f clk_i/o 2 n 1 ocrnx + ( ) ? ? -------------------------------------------------- = tcntn ocrnx update and tovn interrupt flag set 1 period 2 3 ocn ocn (comnx1:0 = 2)(comnx1:0 = 3) ocrnx interrupt flag set 4 5 6 7
68 7598d?avr?02/07 attiny25/45/85 auto in fast pwm mode, the compare unit allows generatio n of pwm waveforms on the oc0x pins. setting the com0x1:0 bits to two will produce a non -inverted pwm and an inverted pwm output can be generated by setting the com0x1:0 to three: setting the com0a1:0 bits to one allowes the ac0a pin to toggle on compare matches if the wg m02 bit is set. this option is not available for the oc0b pin (see table 12-2 on page 72 ). the actual oc0x value will only be visible on th e port pin if the data direction for the port pin is set as output. the pwm waveform is generated by setting (or clearing) the oc0x register at the comp are match between ocr0x and tcnt0, and clearing (or setting) the oc0x register at the time r clock cycle the counter is cleared (changes from top to bottom). the pwm frequency for the output can be calculated by the following equation: the n variable represents the prescale factor (1, 8, 64, 256, or 1024). the extreme values for the ocr0a register represent s special cases when generating a pwm waveform output in the fast pwm mode. if the ocr0a is set equal to bottom, the output will be a narrow spike for each max+1 timer clock cycle. setting the ocr0a equal to max will result in a constantly high or low output (depending on th e polarity of the output set by the com0a1:0 bits.) a frequency (with 50% duty cycle) waveform output i n fast pwm mode can be achieved by set- ting oc0x to toggle its logical level on each compa re match (com0x1:0 = 1). the waveform generated will have a maximum frequency of f oc0 = f clk_i/o /2 when ocr0a is set to zero. this feature is similar to the oc0a toggle in ctc mode, except the double buffer feature of the out- put compare unit is enabled in the fast pwm mode. 12.6.4 phase correct pwm mode the phase correct pwm mode (wgm02:0 = 1 or 5) provi des a high resolution phase correct pwm waveform generation option. the phase correct p wm mode is based on a dual-slope operation. the counter counts repeatedly from botto m to top and then from top to bot- tom. top is defined as 0xff when wgm2:0 = 1, and oc r0a when wgm2:0 = 5. in non- inverting compare output mode, the output compare ( oc0x) is cleared on the compare match between tcnt0 and ocr0x while upcounting, and set o n the compare match while down- counting. in inverting output compare mode, the ope ration is inverted. the dual-slope operation has lower maximum operation frequency than single s lope operation. however, due to the sym- metric feature of the dual-slope pwm modes, these m odes are preferred for motor control applications. in phase correct pwm mode the counter is incremente d until the counter value matches top. when the counter reaches top, it changes the count direction. the tcnt0 value will be equal to top for one timer clock cycle. the timing diagra m for the phase correct pwm mode is shown on figure 12-7 . the tcnt0 value is in the timing diagram shown as a histogram for illustrating the dual-slope operation. the diagram includes non- inverted and inverted pwm outputs. the small horizontal line marks on the tcnt0 slopes rep resent compare matches between ocr0x and tcnt0. f ocnxpwm f clk_i/o n 256 ? ------------------ =
69 7598d?avr?02/07 attiny25/45/85 auto figure 12-7. phase correct pwm mode, timing diagram the timer/counter overflow flag (tov0) is set each time the counter reaches bottom. the interrupt flag can be used to generate an interrupt each time the counter reaches the bottom value. in phase correct pwm mode, the compare unit allows generation of pwm waveforms on the oc0x pins. setting the com0x1:0 bits to two will pr oduce a non-inverted pwm. an inverted pwm output can be generated by setting the com0x1:0 to three: setting the com0a0 bits to one allows the oc0a pin to toggle on compare matche s if the wgm02 bit is set. this option is not available for the oc0b pin (see table 12-3 on page 72 ). the actual oc0x value will only be visible on the port pin if the data direction for t he port pin is set as output. the pwm waveform is generated by clearing (or setting) the oc0x registe r at the compare match between ocr0x and tcnt0 when the counter increments, and setting (or clearing) the oc0x register at com- pare match between ocr0x and tcnt0 when the counter decrements. the pwm frequency for the output when using phase correct pwm can be calc ulated by the following equation: the n variable represents the prescale factor (1, 8 , 64, 256, or 1024). the extreme values for the ocr0a register represent special cases when generating a pwm waveform output in the phase correct pwm mode. if t he ocr0a is set equal to bottom, the output will be continuously low and if set equal to max the output will be continuously high for non-inverted pwm mode. for inverted pwm the output will have the opposite logic values. at the very start of period 2 in figure 12-7 ocn has a transition from high to low even though there is no compare match. the point of this transi tion is to guaratee symmetry around bot- tom. there are two cases that give a transition wit hout compare match. ? ocr0a changes its value from max, like in figure 12-7 . when the ocr0a value is max the ocn pin value is the same as the result of a down-c ounting compare match. to ensure tovn interrupt flag set ocnx interrupt flag set 1 2 3 tcntn period ocn ocn (comnx1:0 = 2)(comnx1:0 = 3) ocrnx update f ocnxpcpwm f clk_i/o n 510 ? ------------------ =
70 7598d?avr?02/07 attiny25/45/85 auto symmetry around bottom the ocn value at max must co rrespond to the result of an up- counting compare match. ? the timer starts counting from a value higher than the one in ocr0a, and for that reason misses the compare match and hence the ocn change t hat would have happened on the way up. 12.7 timer/counter timing diagrams the timer/counter is a synchronous design and the t imer clock (clk t0 ) is therefore shown as a clock enable signal in the following figures. the f igures include information on when interrupt flags are set. figure 12-8 contains timing data for basic timer/counter opera tion. the figure shows the count sequence close to the max value in all modes other than phase correct pwm mode. figure 12-8. timer/counter timing diagram, no prescaling figure 12-9 shows the same timing data, but with the prescaler enabled. figure 12-9. timer/counter timing diagram, with prescaler (f clk_i/o /8) figure 12-10 shows the setting of ocf0b in all modes and ocf0a in all modes except ctc mode and pwm mode, where ocr0a is top. clk tn (clk i/o /1) tovn clk i/o tcntn max - 1 max bottom bottom + 1 tovn tcntn max - 1 max bottom bottom + 1 clk i/o clk tn (clk i/o /8)
71 7598d?avr?02/07 attiny25/45/85 auto figure 12-10. timer/counter timing diagram, setting of ocf0x, wit h prescaler (f clk_i/o /8) figure 12-11 shows the setting of ocf0a and the clearing of tcn t0 in ctc mode and fast pwm mode where ocr0a is top. figure 12-11. timer/counter timing diagram, clear timer on compar e match mode, with pres- caler (f clk_i/o /8) 12.8 8-bit timer/counter register description 12.8.1 timer/counter control register a ? tccr0a ? bits 7:6 ? com01a:0: compare match output a mode these bits control the output compare pin (oc0a) be havior. if one or both of the com0a1:0 bits are set, the oc0a output overrides the normal port functionality of the i/o pin it is connected to. however, note that the data direction register (ddr) bit corresponding to the oc0a pin must be set in order to enable the output driver. ocfnx ocrnx tcntn ocrnx value ocrnx - 1 ocrnx ocrnx + 1 ocrnx + 2 clk i/o clk tn (clk i/o /8) ocfnx ocrnx tcntn (ctc) top top - 1 top bottom bottom + 1 clk i/o clk tn (clk i/o /8) bit 7 6 5 4 3 2 1 0 com0a1 com0a0 com0b1 com0b0 ? ? wgm01 wgm00 tccr0a read/write r/w r/w r/w r/w r r r/w r/w initial value 0 0 0 0 0 0 0 0
72 7598d?avr?02/07 attiny25/45/85 auto when oc0a is connected to the pin, the function of the com0a1:0 bits depends on the wgm02:0 bit setting. table 12-1 shows the com0a1:0 bit functionality when the wgm0 2:0 bits are set to a normal or ctc mode (non-pwm). table 12-2 shows the com0a1:0 bit functionality when the wgm0 1:0 bits are set to fast pwm mode. note: 1. a special case occurs when ocr0a equals top and com0a1 is set. in this case, the com- pare match is ignored, but the set or clear is done at top. see ?fast pwm mode? on page 67 for more details. table 12-3 shows the com0a1:0 bit functionality when the wgm0 2:0 bits are set to phase cor- rect pwm mode. note: 1. a special case occurs when ocr0a equals top and com0a1 is set. in this case, the com- pare match is ignored, but the set or clear is done at top. see ?phase correct pwm mode? on page 68 for more details. ? bits 5:4 ? com0b1:0: compare match output b mode these bits control the output compare pin (oc0b) be havior. if one or both of the com0b1:0 bits are set, the oc0b output overrides the normal port functionality of the i/o pin it is connected to. however, note that the data direction register (ddr) bit corresponding to the oc0b pin must be set in order to enable the output driver. table 12-1. compare output mode, non-pwm mode com01 com00 description 0 0 normal port operation, oc0a disconnected. 0 1 toggle oc0a on compare match 1 0 clear oc0a on compare match 1 1 set oc0a on compare match table 12-2. compare output mode, fast pwm mode (1) com01 com00 description 0 0 normal port operation, oc0a disconnected. 0 1 wgm02 = 0: normal port operation, oc0a disconnected . wgm02 = 1: toggle oc0a on compare match. 1 0 clear oc0a on compare match, set oc0a at top 1 1 set oc0a on compare match, clear oc0a at top table 12-3. compare output mode, phase correct pwm mode (1) com0a1 com0a0 description 0 0 normal port operation, oc0a disconnected. 0 1 wgm02 = 0: normal port operation, oc0a disconnected . wgm02 = 1: toggle oc0a on compare match. 1 0 clear oc0a on compare match when up-counting. set o c0a on compare match when down-counting. 1 1 set oc0a on compare match when up-counting. clear o c0a on compare match when down-counting.
73 7598d?avr?02/07 attiny25/45/85 auto when oc0b is connected to the pin, the function of the com0b1:0 bits depends on the wgm02:0 bit setting. table 12-1 shows the com0a1:0 bit functionality when the wgm0 2:0 bits are set to a normal or ctc mode (non-pwm). table 12-2 shows the com0b1:0 bit functionality when the wgm0 2:0 bits are set to fast pwm mode. note: 1. a special case occurs when ocr0b equals top and com0b1 is set. in this case, the com- pare match is ignored, but the set or clear is done at top. see ?fast pwm mode? on page 67 for more details. table 12-3 shows the com0b1:0 bit functionality when the wgm0 2:0 bits are set to phase cor- rect pwm mode. note: 1. a special case occurs when ocr0b equals top and com0b1 is set. in this case, the com- pare match is ignored, but the set or clear is done at top. see ?phase correct pwm mode? on page 68 for more details. ? bits 3, 2 ? res: reserved bits these bits are reserved bits in the attiny25/45/85 and will always read as zero. ? bits 1:0 ? wgm01:0: waveform generation mode combined with the wgm02 bit found in the tccr0b reg ister, these bits control the counting sequence of the counter, the source for maximum (to p) counter value, and what type of wave- form generation to be used, see table 12-7 . modes of operation supported by the timer/counter table 12-4. compare output mode, non-pwm mode com01 com00 description 0 0 normal port operation, oc0b disconnected. 0 1 toggle oc0b on compare match 1 0 clear oc0b on compare match 1 1 set oc0b on compare match table 12-5. compare output mode, fast pwm mode (1) com01 com00 description 0 0 normal port operation, oc0b disconnected. 0 1 reserved 1 0 clear oc0b on compare match, set oc0b at top 1 1 set oc0b on compare match, clear oc0b at top table 12-6. compare output mode, phase correct pwm mode (1) com0a1 com0a0 description 0 0 normal port operation, oc0b disconnected. 0 1 reserved 1 0 clear oc0b on compare match when up-counting. set o c0b on compare match when down-counting. 1 1 set oc0b on compare match when up-counting. clear o c0b on compare match when down-counting.
74 7598d?avr?02/07 attiny25/45/85 auto unit are: normal mode (counter), clear timer on com pare match (ctc) mode, and two types of pulse width modulation (pwm) modes (see ?modes of operation? on page 65 ). notes: 1. max = 0xff 2. bottom = 0x00 12.8.2 timer/counter control register b ? tccr0b ? bit 7 ? foc0a: force output compare a the foc0a bit is only active when the wgm bits spec ify a non-pwm mode. however, for ensuring compatibility with future dev ices, this bit must be set to zero when tccr0b is written when operating in pwm mode. when writing a logical one to the foc0a bit, an immediate compare match is forced on the wavefor m generation unit. the oc0a output is changed according to its com0a1:0 bits setting. not e that the foc0a bit is implemented as a strobe. therefore it is the value present in the co m0a1:0 bits that determines the effect of the forced compare. a foc0a strobe will not generate any interrupt, nor will it clear the timer in ctc mode using ocr0a as top. the foc0a bit is always read as zero. ? bit 6 ? foc0b: force output compare b the foc0b bit is only active when the wgm bits spec ify a non-pwm mode. however, for ensuring compatibility with future dev ices, this bit must be set to zero when tccr0b is written when operating in pwm mode. when writing a logical one to the foc0b bit, an immediate compare match is forced on the wavefor m generation unit. the oc0b output is changed according to its com0b1:0 bits setting. not e that the foc0b bit is implemented as a table 12-7. waveform generation mode bit description mode wgm2 wgm1 wgm0 timer/counter mode of operation top update of ocrx at tov flag set on (1)(2) 0 0 0 0 normal 0xff immediate max 1 0 0 1 pwm, phase correct 0xff top bottom 2 0 1 0 ctc ocra immediate max 3 0 1 1 fast pwm 0xff top max 4 1 0 0 reserved ? ? ? 5 1 0 1 pwm, phase correct ocra top bottom 6 1 1 0 reserved ? ? ? 7 1 1 1 fast pwm ocra top top bit 7 6 5 4 3 2 1 0 foc0a foc0b ? ? wgm02 cs02 cs01 cs00 tccr0b read/write w w r r r r r/w r/w initial value 0 0 0 0 0 0 0 0
75 7598d?avr?02/07 attiny25/45/85 auto strobe. therefore it is the value present in the co m0b1:0 bits that determines the effect of the forced compare. a foc0b strobe will not generate any interrupt, nor will it clear the timer in ctc mode using ocr0b as top. the foc0b bit is always read as zero. ? bits 5:4 ? res: reserved bits these bits are reserved bits in the attiny25/45/85 and will always read as zero. ? bit 3 ? wgm02: waveform generation mode see the description in the ?timer/counter control register a ? tccr0a? on page 71 . ? bits 2:0 ? cs02:0: clock select the three clock select bits select the clock source to be used by the timer/counter. if external pin modes are used for the timer/counte r0, transitions on the t0 pin will clock the counter even if the pin is configured as an output. this feature allows software control of the counting. 12.8.3 timer/counter register ? tcnt0 the timer/counter register gives direct access, bot h for read and write operations, to the timer/counter unit 8-bit counter. writing to the tc nt0 register blocks (removes) the compare match on the following timer clock. modifying the c ounter (tcnt0) while the counter is running, introduces a risk of missing a compare match betwee n tcnt0 and the ocr0x registers. 12.8.4 output compare register a ? ocr0a table 12-8. clock select bit description cs02 cs01 cs00 description 0 0 0 no clock source (timer/counter stopped) 0 0 1 clk i/o /(no prescaling) 0 1 0 clk i/o /8 (from prescaler) 0 1 1 clk i/o /64 (from prescaler) 1 0 0 clk i/o /256 (from prescaler) 1 0 1 clk i/o /1024 (from prescaler) 1 1 0 external clock source on t0 pin. clock on fallin g edge. 1 1 1 external clock source on t0 pin. clock on rising edge. bit 7 6 5 4 3 2 1 0 tcnt0[7:0] tcnt0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 ocr0a[7:0] ocr0a read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
76 7598d?avr?02/07 attiny25/45/85 auto the output compare register a contains an 8-bit val ue that is continuously compared with the counter value (tcnt0). a match can be used to gener ate an output compare interrupt, or to generate a waveform output on the oc0a pin. 12.8.5 output compare register b ? ocr0b the output compare register b contains an 8-bit val ue that is continuously compared with the counter value (tcnt0). a match can be used to gener ate an output compare interrupt, or to generate a waveform output on the oc0b pin. 12.8.6 timer/counter interrupt mask register ? timsk ? bits 7..4, 0 ? res: reserved bits these bits are reserved bits in the attiny25/45/85 and will always read as zero. ? bit 3 ? ocie0b: timer/counter output compare match b interrupt enable when the ocie0b bit is written to one, and the i-bi t in the status register is set, the timer/counter compare match b interrupt is enabled. the corresponding interrupt is executed if a compare match in timer/counter occurs, i.e., when the ocf0b bit is set in the timer/counter interrupt flag register ? tifr0. ? bit 2 ? ocie0a: timer/counter0 output compare matc h a interrupt enable when the ocie0a bit is written to one, and the i-bi t in the status register is set, the timer/counter0 compare match a interrupt is enabled . the corresponding interrupt is executed if a compare match in timer/counter0 occurs, i.e., when the ocf0a bit is set in the timer/counter 0 interrupt flag register ? tifr0. ? bit 1 ? toie0: timer/counter0 overflow interrupt e nable when the toie0 bit is written to one, and the i-bit in the status register is set, the timer/counter0 overflow interrupt is enabled. the c orresponding interrupt is executed if an overflow in timer/counter0 occurs, i.e., when the t ov0 bit is set in the timer/counter 0 inter- rupt flag register ? tifr0. 12.8.7 timer/counter 0 interrupt flag register ? tif r ? bits 7, 0 ? res: reserved bits these bits are reserved bits in the attiny25/45/85 and will always read as zero. ? bit 4? ocf0a: output compare flag 0 a bit 7 6 5 4 3 2 1 0 ocr0b[7:0] ocr0b read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 ? ocie1a ocie1b ocie0a ocie0b toie1 toie0 ? timsk read/write r r r r r/w r/w r/w r initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 ? ocf1a ocf1b ocf0a ocf0b tov1 tov0 ? tifr read/write r r r r r/w r/w r/w r initial value 0 0 0 0 0 0 0 0
77 7598d?avr?02/07 attiny25/45/85 auto the ocf0a bit is set when a compare match occurs be tween the timer/counter0 and the data in ocr0a ? output compare register0. ocf0a is clear ed by hardware when executing the cor- responding interrupt handling vector. alternatively , ocf0a is cleared by writing a logic one to the flag. when the i-bit in sreg, ocie0a (timer/cou nter0 compare match interrupt enable), and ocf0a are set, the timer/counter0 compare match interrupt is executed. ? bit 3 ? ocf0b: output compare flag 0 b the ocf0b bit is set when a compare match occurs be tween the timer/counter and the data in ocr0b ? output compare register0 b. ocf0b is cleare d by hardware when executing the cor- responding interrupt handling vector. alternatively , ocf0b is cleared by writing a logic one to the flag. when the i-bit in sreg, ocie0b (timer/cou nter compare b match interrupt enable), and ocf0b are set, the timer/counter compare match interrupt is executed. ? bit 1 ? tov0: timer/counter0 overflow flag the bit tov0 is set when an overflow occurs in time r/counter0. tov0 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, tov0 is cleared by writing a logic one to the flag. when the sreg i-bi t, toie0 (timer/counter0 overflow interrupt enable), and tov0 are set, the timer/counter0 overf low interrupt is executed. the setting of this flag is dependent of the wgm02: 0 bit setting. refer to table 12-7 , ?waveform generation mode bit description? on page 74 . 13. timer/counter prescaler the timer/counter can be clocked directly by the sy stem clock (by setting the csn2:0 = 1). this provides the fastest operation, with a maximum time r/counter clock frequency equal to system clock frequency (f clk_i/o ). alternatively, one of four taps from the prescal er can be used as a clock source. the prescaled clock has a frequency o f either f clk_i/o /8, f clk_i/o /64, f clk_i/o /256, or f clk_i/o /1024. 13.0.1 prescaler reset the prescaler is free running, i.e., operates indep endently of the clock select logic of the timer/counter. since the prescaler is not affected by the timer/counter?s clock select, the state of the prescaler will have implications for situati ons where a prescaled clock is used. one exam- ple of prescaling artifacts occurs when the timer i s enabled and clocked by the prescaler (6 > csn2:0 > 1). the number of system clock cycles from when the timer is enabled to the first count occurs can be from 1 to n+1 system clock cycles, wh ere n equals the prescaler divisor (8, 64, 256, or 1024). it is possible to use the prescaler reset for synch ronizing the timer/counter to program execution. 13.0.2 external clock source an external clock source applied to the t0 pin can be used as timer/counter clock (clk t0 ). the t0 pin is sampled once every system clock cycle by the pin synchronization logic. the synchro- nized (sampled) signal is then passed through the e dge detector. figure 13-1 shows a functional equivalent block diagram of the t0 synchronization and edge detector logic. the registers are clocked at the positive edge of the internal system clock ( clk i/o ). the latch is transparent in the high period of the internal system clock.
78 7598d?avr?02/07 attiny25/45/85 auto the edge detector generates one clk t 0 pulse for each positive (csn2:0 = 7) or negative ( csn2:0 = 6) edge it detects. figure 13-1. t0 pin sampling the synchronization and edge detector logic introdu ces a delay of 2.5 to 3.5 system clock cycles from an edge has been applied to the t0 pin to the counter is updated. enabling and disabling of the clock input must be d one when t0 has been stable for at least one system clock cycle, otherwise it is a risk that a f alse timer/counter clock pulse is generated. each half period of the external clock applied must be longer than one system clock cycle to ensure correct sampling. the external clock must be guaranteed to have less than half the sys- tem clock frequency (f extclk < f clk_i/o /2) given a 50/50% duty cycle. since the edge detec tor uses sampling, the maximum frequency of an external cloc k it can detect is half the sampling fre- quency (nyquist sampling theorem). however, due to variation of the system clock frequency and duty cycle caused by oscillator source (crystal , resonator, and capacitors) tolerances, it is recommended that maximum frequency of an external c lock source is less than f clk_i/o /2.5. an external clock source can not be prescaled. figure 13-2. prescaler for timer/counter0 note: 1. the synchronization logic on the input pins ( t0) is shown in figure 13-1 . 13.0.3 general timer/counter control register ? gtcc r tn_sync (to clock select logic) edge detector synchronization d q d q le d q tn clk i/o psr10 clear clk t0 t0 clk i/o synchronization bit 7 6 5 4 3 2 1 0 tsm pwm1b com1b1 com1b0 foc1b foc1a psr1 psr0 gtccr
79 7598d?avr?02/07 attiny25/45/85 auto ? bit 7 ? tsm: timer/counter synchronization mode writing the tsm bit to one activates the timer/coun ter synchronization mode. in this mode, the value that is written to the psr0 bit is kept, henc e keeping the prescaler reset signal asserted. this ensures that the timer/counter is halted and c an be configured without the risk of advanc- ing during configuration. when the tsm bit is writt en to zero, the psr0 bit is cleared by hardware, and the timer/counter start counting. ? bit 0 ? psr0: prescaler reset timer/counter0 when this bit is one, the timer/counter0 prescaler will be reset. this bit is normally cleared immediately by hardware, except if the tsm bit is s et. 14. 8-bit timer/counter1 the timer/counter1 is a general purpose 8-bit timer /counter module that has a separate pres- caling selection from the separate prescaler. figure 14-1 shows the timer/counter1 prescaler that supports t wo clocking modes, a syncrho- nous clocking mode and an asynchronous clocking mod e. the synchronous clocking mode uses the system clock (ck) as the clock timebase and asy nchronous mode uses the fast peripheral clock (pck) as the clock time base. the pcke bit fr om the pllcsr register enables the asyn- chronous mode when it is set (?1?). figure 14-1. timer/counter1 prescaler in the asynchronous clocking mode the clock selecti ons are from pck to pck/16384 and stop, and in the synchronous clocking mode the clock sele ctions are from ck to ck/16384 and stop. the clock options are described in table 14-2 on page 83 and the timer/counter1 control reg- ister, tccr1. setting the psr1 bit in gtccr registe r resets the prescaler. the pcke bit in the pllcsr register enables the asynchronous mode. the frequency of the fast peripheral clock is 64 mhz (or 32 mhz in low speed mode). read/write r/w r r r r r r r/w initial value 0 0 0 0 0 0 0 0 timer/counter1 count enable psr1 cs10cs11 cs12 pck 64/32 mhz 0 cs13 14-bit t/c prescaler t1ck/2 t1ck t1ck/4 t1ck/8 t1ck/16 t1ck/32 t1ck/64 t1ck/128 t1ck/256 t1ck/512 t1ck/1024 t1ck/2048 t1ck/4096 t1ck/8192 t1ck/16384 s a ck pcke t1ck
80 7598d?avr?02/07 attiny25/45/85 auto 14.1 timer/counter1 the timer/counter1 general operation is described i n the asynchronous mode and the opera- tion in the synchronous mode is mentioned only if t here are differences between these two modes. figure 14-2 shows timer/counter 1 synchronization register blo ck diagram and syn- chronization delays in between registers. note that all clock gating details are not shown in the figure. the timer/counter1 register values go throu gh the internal synchronization registers, which cause the input synchronization delay, before affecting the counter operation. the regis- ters tccr1, gtccr, ocr1a, ocr1b, and ocr1c can be r ead back right after writing the register. the read back values are delayed for the timer/counter1 (tcnt1) register and flags (ocf1a, ocf1b, and tov1), because of the input and output synchronization. the timer/counter1 features a high resolution and a high accuracy usage with the lower pres- caling opportunities. it can also support two accur ate, high speed, 8-bit pulse width modulators using clock speeds up to 64 mhz ( or 32 mhz in low speed mode). in this mode, timer/counter1 and the output compare registers ser ve as dual stand-alone pwms with non- overlapping non-inverted and inverted outputs. refe r to page 88 for a detailed description on this function. similarly, the high prescaling oppor tunities make this unit useful for lower speed functions or exact timing functions with infrequent actions. figure 14-2. timer/counter 1 synchronization register block diag ram. 8-bit databus ocr1a ocr1a_si tcnt_so ocr1b ocr1b_si ocr1c ocr1c_si tccr1 tccr1_si gtccr gtccr_si tcnt1 tcnt1_si ocf1a ocf1a_si ocf1b ocf1b_si tov1 tov1_si tov1_so ocf1b_so ocf1a_so tcnt1 s a s a pckeck pck io-registers input synchronization registers timer/counter1 output synchronization registers sync mode async mode 1 ck delay no delay ~1/2 ck delay 1 pck delay 1/2 pck - 1 ck delay no delay tcnt1ocf1a ocf1b tov1 1/2 ck delay 1/2 ck delay
81 7598d?avr?02/07 attiny25/45/85 auto timer/counter1 and the prescaler allow running the cpu from any clock source while the pres- caler is operating on the fast 64 mhz (or 32 mhz in low speed mode) pck clock in the asynchronous mode. note that the system clock frequency must be lower than one third of the pck frequency. the synchronization mechanism of the asynchronous timer /counter1 needs at least two edges of the pck when the system clock is high. if the frequ ency of the system clock is too high, it is a risk that data or control values are lost. the following figure 14-3 shows the block diagram for timer/counter1. figure 14-3. timer/counter1 block diagram three status flags (overflow and compare matches) a re found in the timer/counter interrupt flag register - tifr. control signals are found in the timer/counter control registers tccr1 and gtccr. the interrupt enable/disable settings ar e found in the timer/counter interrupt mask register - timsk. the timer/counter1 contains three output compare re gisters, ocr1a, ocr1b, and ocr1c as the data source to be compared with the timer/co unter1 contents. in normal mode the out- put compare functions are operational with all thre e output compare registers. ocr1a determines action on the oc1a pin (pb1), and it can generate timer1 oc1a interrupt in normal mode and in pwm mode. likewise, ocr1b determines ac tion on the oc1b pin (pb3) and it can generate timer1 oc1b interrupt in normal mode and i n pwm mode. ocr1c holds the timer/counter maximum value, i.e. the clear on comp are match value. in the normal mode an overflow interrupt (tov1) is generated when timer/c ounter1 counts from $ff to $00, while in the pwm mode the overflow interrupt is generated wh en timer/counter1 counts either from $ff 8-bit databus timer int. flag register (tifr) timer/counter1 8-bit comparator t/c1 output compare register timer int. mask register (timsk) timer/counter1 (tcnt1) t/c clear t/c1 control logic tov1 ocf1b ocf1b tov1 toie0 toie1 ocie1b ocie1a ocf1a ocf1a ck pck t/c1 over- flow irq t/c1 compare match b irq oc1a (pb1) t/c1 compare match a irq t/c control register 1 (tccr1) com1b1 pwm1a pwm1b com1b0 foc1a foc1b (ocr1a) (ocr1b) (ocr1c) 8-bit comparator t/c1 output compare register tov0 com1a1 com1a0 8-bit comparator t/c1 output compare register global t/c control register (gtccr) cs12 psr1 cs11 cs10 cs13 ctc1 oc1a (pb0) oc1b (pb4) oc1b (pb3) dead time generator dead time generator
82 7598d?avr?02/07 attiny25/45/85 auto to $00 or from ocr1c to $00. the inverted pwm outpu ts oc1a and oc1b are not connected in normal mode. in pwm mode, ocr1a and ocr1b provide the data value s against which the timer counter value is compared. upon compare match the pwm outpu ts (oc1a, oc1a , oc1b, oc1b ) are generated. in pwm mode, the timer counter counts up to the value specified in the output com- pare register ocr1c and starts again from $00. this feature allows limiting the counter ?full? value to a specified value, lower than $ff. togethe r with the many prescaler options, flexible pwm frequency selection is provided. table 14-6 lists clock selection and ocr1c values to obtain pwm frequencies from 20 khz to 250 khz in 10 khz steps and from 250 khz to 500 khz in 50 khz steps. higher pwm frequencies can be obta ined at the expense of resolution. 14.1.1 timer/counter1 control register - tccr1 ? bit 7- ctc1 : clear timer/counter on compare match when the ctc1 control bit is set (one), timer/count er1 is reset to $00 in the cpu clock cycle after a compare match with ocr1c register value. if the control bit is cleared, timer/counter1 continues counting and is unaffected by a compare m atch. ? bit 6- pwm1a: pulse width modulator a enable when set (one) this bit enables pwm mode based on c omparator ocr1a in timer/counter1 and the counter value is reset to $00 in the cpu cl ock cycle after a compare match with ocr1c register value. ? bits 5,4 - com1a1, com1a0: comparator a output mod e, bits 1 and 0 the com1a1 and com1a0 control bits determine any ou tput pin action following a compare match with compare register a in timer/counter1. ou tput pin actions affect pin pb1 (oc1a). since this is an alternative function to an i/o por t, the corresponding direction control bit must be set (one) in order to control an output pin. note t hat oc1a is not connected in normal mode. in pwm mode, these bits have different functions. r efer to table 14-4 on page 88 for a detailed description. ? bits 3 .. 0 - cs13, cs12, cs11, cs10: clock select bits 3, 2, 1, and 0 bit 7 6 5 4 3 2 1 0 $30 ($50) ctc1 pwm1a com1a1 com1a0 cs13 cs12 cs11 cs10 tccr1 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 table 14-1. comparator a mode select com1a1 com1a0 description 0 0 timer/counter comparator a disconnected from outp ut pin oc1a. 0 1 toggle the oc1a output line. 1 0 clear the oc1a output line. 1 1 set the oc1a output line
83 7598d?avr?02/07 attiny25/45/85 auto the clock select bits 3, 2, 1, and 0 define the pre scaling source of timer/counter1. the stop condition provides a timer enable/disable function. 14.1.2 general timer/counter1 control register - gtc cr ? bit 6- pwm1b: pulse width modulator b enable when set (one) this bit enables pwm mode based on c omparator ocr1b in timer/counter1 and the counter value is reset to $00 in the cpu cl ock cycle after a compare match with ocr1c register value. ? bits 5,4 - com1b1, com1b0: comparator b output mod e, bits 1 and 0 the com1b1 and com1b0 control bits determine any ou tput pin action following a compare match with compare register b in timer/counter1. ou tput pin actions affect pin pb3 (oc1b). table 14-2. timer/counter1 prescale select cs13 cs12 cs11 cs10 asynchronous clocking mode synchronous clocking mode 0 0 0 0 t/c1 stopped t/c1 stopped 0 0 0 1 pck ck 0 0 1 0 pck/2 ck/2 0 0 1 1 pck/4 ck/4 0 1 0 0 pck/8 ck/8 0 1 0 1 pck/16 ck/16 0 1 1 0 pck/32 ck/32 0 1 1 1 pck/64 ck/64 1 0 0 0 pck/128 ck/128 1 0 0 1 pck/256 ck/256 1 0 1 0 pck/512 ck/512 1 0 1 1 pck/1024 ck/1024 1 1 0 0 pck/2048 ck/2048 1 1 0 1 pck/4096 ck/4096 1 1 1 0 pck/8192 ck/8192 1 1 1 1 pck/16384 ck/16384 bit 7 6 5 4 3 2 1 0 $2c ($4c) tsm pwm1b com1b1 com1b0 foc1b foc1a psr1 psr0 gtccr read/write r/w r/w r/w r/w w w r/w r/w initial value 0 0 0 0 0 0 0 0
84 7598d?avr?02/07 attiny25/45/85 auto since this is an alternative function to an i/o por t, the corresponding direction control bit must be set (one) in order to control an output pin. note t hat oc1b is not connected in normal mode. in pwm mode, these bits have different functions. r efer to table 14-4 on page 88 for a detailed description. ? bit 3- foc1b: force output compare match 1b writing a logical one to this bit forces a change i n the compare match output pin pb3 (oc1b) according to the values already set in com1b1 and c om1b0. if com1b1 and com1b0 written in the same cycle as foc1b, the new settings will b e used. the force output compare bit can be used to change the output pin value regardless o f the timer value. the automatic action pro- grammed in com1b1 and com1b0 takes place as if a co mpare match had occurred, but no interrupt is generated. the foc1b bit always reads as zero. foc1b is not in use if pwm1b bit is set. ? bit 2- foc1a: force output compare match 1a writing a logical one to this bit forces a change i n the compare match output pin pb1 (oc1a) according to the values already set in com1a1 and c om1a0. if com1a1 and com1a0 written in the same cycle as foc1a, the new settings will b e used. the force output compare bit can be used to change the output pin value regardless o f the timer value. the automatic action pro- grammed in com1a1 and com1a0 takes place as if a co mpare match had occurred, but no interrupt is generated. the foc1a bit always reads as zero. foc1a is not in use if pwm1a bit is set. ? bit 1- psr1 : prescaler reset timer/counter1 when this bit is set (one), the timer/counter presc aler (tcnt1 is unaffected) will be reset. the bit will be cleared by hardware after the operation is performed. writing a zero to this bit will have no effect. this bit will always read as zero. 14.1.3 timer/counter1 - tcnt1 this 8-bit register contains the value of timer/cou nter1. timer/counter1 is realized as an up counter with re ad and write access. due to synchronization of the cpu, timer/counter1 data written into timer/ counter1 is delayed by one and half cpu clock cycles in synchronous mode and at most one cp u clock cycles for asynchronous mode. table 14-3. comparator b mode select com1b1 com1b0 description 0 0 timer/counter comparator b disconnected from outp ut pin oc1b. 0 1 toggle the oc1b output line. 1 0 clear the oc1b output line. 1 1 set the oc1b output line bit 7 6 5 4 3 2 1 0 $2f ($4f) msb lsb tcnt1 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
85 7598d?avr?02/07 attiny25/45/85 auto 14.1.4 timer/counter1 output compare registera - ocr 1a the output compare register a is an 8-bit read/writ e register. the timer/counter output compare register a contain s data to be continuously compared with timer/counter1. actions on compare matches are spec ified in tccr1. a compare match does only occur if timer/counter1 counts to the ocr1a va lue. a software write that sets tcnt1 and ocr1a to the same value does not generate a compare match. a compare match will set the compare interrupt flag ocf1a after a synchronization delay follow- ing the compare event. 14.1.5 timer/counter1 output compare registerb - ocr 1b the output compare register b is an 8-bit read/writ e register. the timer/counter output compare register b contain s data to be continuously compared with timer/counter1. actions on compare matches are spec ified in tccr1. a compare match does only occur if timer/counter1 counts to the ocr1b va lue. a software write that sets tcnt1 and ocr1b to the same value does not generate a compare match. a compare match will set the compare interrupt flag ocf1b after a synchronization delay follow- ing the compare event. 14.1.6 timer/counter1 output compare registerc - ocr 1c the output compare register c is an 8-bit read/writ e register. the timer/counter output compare register c contain s data to be continuously compared with timer/counter1. a compare match does only occur if timer/counter1 counts to the ocr1c value. a software write that sets tcnt1 and ocr1c t o the same value does not generate a compare match. if the ctc1 bit in tccr1 is set, a c ompare match will clear tcnt1. this register has the same function in normal mode and pwm mode. 14.1.7 timer/counter interrupt mask register - timsk bit 7 6 5 4 3 2 1 0 $2e ($4e) msb lsb ocr1a read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 $2d ($4d) msb lsb ocr1b read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 $2b ($4b) msb lsb ocr1c read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 1 1 1 1 1 1 1 1 bit 7 6 5 4 3 2 1 0 $39 ($59) - ocie1a ocie1b ocie0a ocie0b toie1 toie0 - timsk read/write r r/w r/w r r r/w r/w r initial value 0 0 0 0 0 0 0 0
86 7598d?avr?02/07 attiny25/45/85 auto ? bit 7 - res: reserved bit this bit is a reserved bit in the attiny25/45/85 an d always reads as zero. ? bit 6 - ocie1a: timer/counter1 output compare inte rrupt enable when the ocie1a bit is set (one) and the i-bit in t he status register is set (one), the timer/counter1 compare matcha, interrupt is enabled . the corresponding interrupt at vector $003 is executed if a compare matcha occurs. the co mpare flag in timer/counter1 is set (one) in the timer/counter interrupt flag register. ? bit 5 - ocie1b: timer/counter1 output compare inte rrupt enable when the ocie1b bit is set (one) and the i-bit in t he status register is set (one), the timer/counter1 compare matchb, interrupt is enabled . the corresponding interrupt at vector $009 is executed if a compare matchb occurs. the co mpare flag in timer/counter1 is set (one) in the timer/counter interrupt flag register. ? bit 2 - toie1: timer/counter1 overflow interrupt e nable when the toie1 bit is set (one) and the i-bit in th e status register is set (one), the timer/counter1 overflow interrupt is enabled. the c orresponding interrupt (at vector $004) is executed if an overflow in timer/counter1 occurs. t he overflow flag (timer1) is set (one) in the timer/counter interrupt flag register - tifr. ? bit 0 - res: reserved bit this bit is a reserved bit in the attiny25/45/85 an d always reads as zero. 14.1.8 timer/counter interrupt flag register - tifr ? bit 7 - res: reserved bit this bit is a reserved bit in the attiny25/45/85 an d always reads as zero. ? bit 6 - ocf1a: output compare flag 1a the ocf1a bit is set (one) when compare match occur s between timer/counter1 and the data value in ocr1a - output compare register 1a. ocf1a is cleared by hardware when executing the corresponding interrupt handling vector. altern atively, ocf1a is cleared, after synchroniza- tion clock cycle, by writing a logic one to the fla g. when the i-bit in sreg, ocie1a, and ocf1a are set (one), the timer/counter1 a compare match i nterrupt is executed. ? bit 5 - ocf1b: output compare flag 1b the ocf1b bit is set (one) when compare match occur s between timer/counter1 and the data value in ocr1b - output compare register 1a. ocf1b is cleared by hardware when executing the corresponding interrupt handling vector. altern atively, ocf1b is cleared, after synchroniza- tion clock cycle, by writing a logic one to the fla g. when the i-bit in sreg, ocie1b, and ocf1b are set (one), the timer/counter1 b compare match i nterrupt is executed. ? bit 2 - tov1: timer/counter1 overflow flag bit 7 6 5 4 3 2 1 0 $38 ($58) - ocf1a ocf1b ocf0a ocf0b tov1 tov0 - tifr read/write r r/w r/w r r r/w r/w r initial value 0 0 0 0 0 0 0 0
87 7598d?avr?02/07 attiny25/45/85 auto in normal mode (pwm1a=0 and pwm1b=0) the bit tov1 i s set (one) when an overflow occurs in timer/counter1. the bit tov1 is cleared by hardw are when executing the corresponding interrupt handling vector. alternatively, tov1 is c leared, after synchronization clock cycle, by writing a logical one to the flag. in pwm mode (either pwm1a=1 or pwm1b=1) the bit tov 1 is set (one) when compare match occurs between timer/counter1 and data value in ocr 1c - output compare register 1c. clearing the timer/counter1 with the bit ctc1 does not generate an overflow. when the sreg i-bit, and toie1 (timer/counter1 over flow interrupt enable), and tov1 are set (one), the timer/counter1 overflow interrupt is exe cuted. ? bit 0 - res: reserved bit this bit is a reserved bit in the attiny25/45/85 an d always reads as zero. 14.1.9 pll control and status register - pllcsr ? bit 7- lsm: low speed mode the high speed mode is enabled as default and the f ast peripheral clock is 64 mhz, but the low speed mode can be set by writing the lsm bit to one . then the fast peripheral clock is scaled down to 32 mhz. the low speed mode must be set, if the supply voltage is below 2.7 volts, because the timer/counter1 is not running fast enou gh on low voltage levels. it is highly recom- mended that timer/counter1 is stopped whenever the lsm bit is changed. ? bit 6.. 3- res : reserved bits these bits are reserved bits in the attiny25/45/85 and always read as zero. ? bit 2- pcke: pck enable the pcke bit change the timer/counter1 clock source . when it is set, the asynchronous clock mode is enabled and fast 64 mhz (or 32 mhz in low s peed mode) pck clock is used as timer/counter1 clock source. if this bit is cleared , the synchronous clock mode is enabled, and system clock ck is used as timer/counter1 clock sou rce. this bit can be set only if plle bit is set. it is safe to set this bit only when the pll i s locked i.e the plock bit is 1. the bit pcke can only be set, if the pll has been enabled earlier. ? bit 1- plle: pll enable when the plle is set, the pll is started and if nee ded internal rc-oscillator is started as a pll reference clock. if pll is selected as a system clo ck source the value for this bit is always 1. ? bit 0- plock: pll lock detector when the plock bit is set, the pll is locked to the reference clock, and it is safe to enable pck for timer/counter1. after the pll is enabled, it ta kes about 100 micro seconds for the pll to lock. bit 7 6 5 4 3 2 1 0 $27 ($27) lsm - - - - pcke plle plock pllcsr read/write r r r r r r/w r/w r initial value 0 0 0 0 0 0 0/1 0
88 7598d?avr?02/07 attiny25/45/85 auto 14.1.10 timer/counter1 initialization for asynchrono us mode to change timer/counter1 to the asynchronous mode, first enable pll, and poll the plock bit until it is set, and then set the pcke bit. 14.1.11 timer/counter1 in pwm mode when the pwm mode is selected, timer/counter1 and t he output compare register c - ocr1c form a dual 8-bit, free-running and glitch-fr ee pwm generator with outputs on the pb1(oc1a) and pb3(oc1b) pins and inverted outputs o n pins pb0(oc1a ) and pb2(oc1b ). as default non-overlapping times for complementary out put pairs are zero, but they can be inserted using a dead time generator (see description on pag e 100). figure 14-4. the pwm output pair when the counter value match the contents of ocr1a or ocr1b, the oc1a and oc1b outputs are set or cleared according to the com1a1/com1a0 o r com1b1/com1b0 bits in the timer/counter1 control register a - tccr1, as shown in table 14-4 . timer/counter1 acts as an up-counter, counting from $00 up to the value specified in the output compare register ocr1c, and starting from $00 up ag ain. a compare match with oc1c will set an overflow interrupt flag (tov1) after a synchroni zation delay following the compare event. note that in pwm mode, writing to the output compar e registers ocr1a or ocr1b, the data value is first transferred to a temporary location. the value is latched into ocr1a or ocr1b when the timer/counter reaches ocr1c. this prevents the occurrence of odd-length pwm pulses (glitches) in the event of an unsynchronized ocr1a or ocr1b. see figure 14-5 for an example. table 14-4. compare mode select in pwm mode com11 com10 effect on output compare pins 0 0 oc1x not connected. oc1x not connected. 0 1 oc1x cleared on compare match. set whentcnt1 = $01. oc1x set on compare match. cleared when tcnt1 = $00. 1 0 oc1x cleared on compare match. set when tcnt1 = $01 . oc1x not connected. 1 1 oc1x set on compare match. cleared when tcnt1= $01. oc1x not connected. pwm1x pwm1x x = a or b t non-overlap =0 t non-overlap =0
89 7598d?avr?02/07 attiny25/45/85 auto figure 14-5. effects of unsynchronized ocr latching during the time between the write and the latch ope ration, a read from ocr1a or ocr1b will read the contents of the temporary location. this m eans that the most recently written value always will read out of ocr1a or ocr1b. when ocr1a or ocr1b contain $00 or the top value, a s specified in ocr1c register, the out- p u t p b 1 ( o c 1 a ) o r p b 3 ( o c 1 b ) i s h e l d l o w o r h i g h a c c o r d i n g t o t h e s e t t i n g s o f com1a1/com1a0. this is shown in table 14-5 . in pwm mode, the timer overflow flag - tov1 is set when the tcnt1 counts to the ocr1c value and the tcnt1 is reset to $00. the timer over flow interrupt1 is executed when tov1 is set provided that timer overflow interrupt and glob al interrupts are enabled. this also applies to the timer output compare flags and interrupts. the frequency of the pwm will be timer clock 1 freq uency divided by (ocr1c value + 1). see the following equation: table 14-5. pwm outputs ocr1x = $00 or ocr1c, x = a or b com1x1 com1x0 ocr1x output oc1x output oc1x 0 1 $00 l h 0 1 ocr1c h l 1 0 $00 l not connected. 1 0 ocr1c h not connected. 1 1 $00 h not connected. 1 1 ocr1c l not connected. pwm output oc1xpwm output oc1x unsynchronized oc1x latch synchronized oc1x latch counter valuecompare value counter value compare value compare value changes glitch compare value changes f pwm f tck1 ocr1c + 1 ( ) ------------------------------------ =
90 7598d?avr?02/07 attiny25/45/85 auto resolution shows how many bit is required to expres s the value in the ocr1c register. it is cal- culated by following equation resolution pwm = log 2 (ocr1c + 1). 15. 8-bit timer/counter1 in attiny15 mode the attiny15 compatibility mode is selected by writ ing the code ?0011? to the cksel fuses (if any other code is written, the timer/counter1 is wo rking in normal mode). when selected the attiny15 compatibility mode provides an attiny15 ba ckward compatible prescaler and timer/counter. furthermore, the clocking system has same clock frequencies as in attiny15. table 14-6. timer/counter1 clock prescale select in the asynchr onous mode pwm frequency clock selection cs13..cs10 ocr1c resoluti on 20 khz pck/16 0101 199 7.6 30 khz pck/16 0101 132 7.1 40 khz pck/8 0100 199 7.6 50 khz pck/8 0100 159 7.3 60 khz pck/8 0100 132 7.1 70 khz pck/4 0011 228 7.8 80 khz pck/4 0011 199 7.6 90 khz pck/4 0011 177 7.5 100 khz pck/4 0011 159 7.3 110 khz pck/4 0011 144 7.2 120 khz pck/4 0011 132 7.1 130 khz pck/2 0010 245 7.9 140 khz pck/2 0010 228 7.8 150 khz pck/2 0010 212 7.7 160 khz pck/2 0010 199 7.6 170 khz pck/2 0010 187 7.6 180 khz pck/2 0010 177 7.5 190 khz pck/2 0010 167 7.4 200 khz pck/2 0010 159 7.3 250 khz pck 0001 255 8.0 300 khz pck 0001 212 7.7 350 khz pck 0001 182 7.5 400 khz pck 0001 159 7.3 450 khz pck 0001 141 7.1 500 khz pck 0001 127 7.0
91 7598d?avr?02/07 attiny25/45/85 auto 15.1 timer/counter1 prescaler figure 15-1 shows an attiny15 compatible prescaler. it has two prescaler units, the 3-bit pres- caler for the the system clock (ck) and the 10-bit prescaler for the fast peripheral clock (pck). the clocking system of the timer/counter1 is always synchronous in the attiny15 compatibility mode, because the same rc oscillator is used as a p ll clock source (generates the input clock for the prescaler) and the avr core. figure 15-1. timer/counter1 prescaler the same clock selections as in attiny15 can be cho sen for timer/counter1 from the output multiplexer, because the frequency of the fast peri pheral clock is 25.6 mhz and the prescaler is similar in the attiny15 compatibility mode. the clo ck selections are pck, pck/2, pck/4, pck/8, ck, ck/2, ck/4, ck/8, ck/16, ck/32, ck/64, ck/128, ck/256, ck/512, ck/1024 and stop. 15.2 timer/counter1 figure 15-2 shows timer/counter 1 synchronization register blo ck diagram and synchronization delays in between registers. note that all clock ga ting details are not shown in the figure. the timer/counter1 register values go through the inter nal synchronization registers, which cause the input synchronization delay, before affecting t he counter operation. the registers tccr1, gtccr, ocr1a and ocr1c can be read back right after writing the register. the read back values are delayed for the timer/counter1 (tcnt1) r egister and flags (ocf1a and tov1), because of the input and output synchronization. the timer/counter1 features a high resolution and a high accuracy usage with the lower pres- caling opportunities. it can also support an accura te, high speed, 8-bit pulse width modulator (pwm) using clock speeds up to 25.6 mhz. in this mo de, timer/counter1 and the output com- pare registers serve as a stand-alone pwm. refer to page 98 for a detailed description on this function. similarly, the high prescaling opportunit ies make this unit useful for lower speed func- tions or exact timing functions with infrequent act ions. timer/counter1 count enable psr1 cs10cs11 cs12 pck (25.6 mhz) 0 cs13 3-bit t/c prescaler pck/2 pck pck/4 pck/8 ck/2 ck/4 ck/8 ck/16 ck/32 ck/64 ck/128 ck/256 ck/512 ck/1024 10-bit t/c prescaler ck (1.6 mhz) ck clear clear
92 7598d?avr?02/07 attiny25/45/85 auto figure 15-2. timer/counter 1 synchronization register block diag ram. timer/counter1 and the prescaler allow running the cpu from any clock source while the pres- caler is operating on the fast 25.6 mhz pck clock i n the asynchronous mode. the following figure 15-3 shows the block diagram for timer/counter1. 8-bit databus ocr1a ocr1a_si tcnt_so ocr1c ocr1c_si tccr1 tccr1_si gtccr gtccr_si tcnt1 tcnt1_si ocf1a ocf1a_si tov1 tov1_si tov1_so ocf1a_so tcnt1 s a s a pckeck pck io-registers input synchronization registers timer/counter1 output synchronization registers sync mode async mode 1ck delay no delay ~1/2 ck delay 1pck delay 1/2pck - 1ck delay no delay tcnt1ocf1a tov1 1/2 ck delay 1/2 ck delay
93 7598d?avr?02/07 attiny25/45/85 auto figure 15-3. timer/counter1 block diagram two status flags (overflow and compare match) are f ound in the timer/counter interrupt flag register - tifr. control signals are found in the t imer/counter control registers tccr1 and gtccr. the interrupt enable/disable settings are fo und in the timer/counter interrupt mask register - timsk. the timer/counter1 contains two output compare regi sters, ocr1a and ocr1c as the data source to be compared with the timer/counter1 conte nts. in normal mode the output compare functions are operational with ocr1a only. ocr1a de termines action on the oc1a pin (pb1), and it can generate timer1 oc1a interrupt in normal mode and in pwm mode. ocr1c holds the timer/counter maximum value, i.e. the clear on compare match value. in the normal mode an overflow interrupt (tov1) is generated when time r/counter1 counts from $ff to $00, while in the pwm mode the overflow interrupt is generated when the timer/counter1 counts either from $ff to $00 or from ocr1c to $00. in pwm mode, ocr1a provides the data values against which the timer counter value is com- pared. upon compare match the pwm outputs (oc1a) is generated. in pwm mode, the timer counter counts up to the value specified in the out put compare register ocr1c and starts again from $00. this feature allows limiting the counter ?full? value to a specified value, lower than $ff. together with the many prescaler options, flexible pwm frequency selection is provided. table 14-6 lists clock selection and ocr1c values to obtain p wm frequencies from 20 khz to 250 khz in 10 khz steps and from 250 khz to 500 khz in 50 k hz steps. higher pwm frequencies can be obtained at the expense of resolution. 8-bit databus timer int. flag register (tifr) timer/counter1 8-bit comparator t/c1 output compare register timer int. mask register (timsk) timer/counter1 (tcnt1) t/c clear t/c1 control logic tov1 tov1 toie0 toie1 ocie1a ocf1a ocf1a ck pck t/c1 over- flow irq oc1a (pb1) t/c1 compare match a irq global t/c control register 2 (gtccr) pwm1a foc1a (ocr1a) (ocr1c) 8-bit comparator t/c1 output compare register tov0 com1a1 com1a0 t/c control register 1 (tccr1) cs12 psr1 cs11 cs10 cs13 ctc1
94 7598d?avr?02/07 attiny25/45/85 auto 15.2.1 15.2.2 timer/counter1 control register - tccr1 ? bit 7- ctc1 : clear timer/counter on compare match when the ctc1 control bit is set (one), timer/count er1 is reset to $00 in the cpu clock cycle after a compare match with ocr1a register. if the c ontrol bit is cleared, timer/counter1 contin- ues counting and is unaffected by a compare match. ? bit 6- pwm1a: pulse width modulator a enable when set (one) this bit enables pwm mode based on c omparator ocr1a in timer/counter1 and the counter value is reset to $00 in the cpu cl ock cycle after a compare match with ocr1c register value. ? bits 5,4 - com1a1, com1a0: comparator a output mod e, bits 1 and 0 the com1a1 and com1a0 control bits determine any ou tput pin action following a compare match with compare register a in timer/counter1. ou tput pin actions affect pin pb1 (oc1a). since this is an alternative function to an i/o por t, the corresponding direction control bit must be set (one) in order to control an output pin. in pwm mode, these bits have different functions. r efer to table 15-3 on page 98 for a detailed description. ? bits 3 .. 0 - cs13, cs12, cs11, cs10: clock select bits 3, 2, 1, and 0 the clock select bits 3, 2, 1, and 0 define the pre scaling source of timer/counter1. bit 7 6 5 4 3 2 1 0 $30 ($50) ctc1 pwm1a com1a1 com1a0 cs13 cs12 cs11 cs10 tccr1a read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 table 15-1. comparator a mode select com1a1 com1a0 description 0 0 timer/counter comparator a disconnected from outp ut pin oc1a. 0 1 toggle the oc1a output line. 1 0 clear the oc1a output line. 1 1 set the oc1a output line table 15-2. timer/counter1 prescale select cs13 cs12 cs11 cs10 t/c1 clock 0 0 0 0 t/c1 stopped 0 0 0 1 pck 0 0 1 0 pck/2 0 0 1 1 pck/4 0 1 0 0 pck/8 0 1 0 1 ck 0 1 1 0 ck/2
95 7598d?avr?02/07 attiny25/45/85 auto the stop condition provides a timer enable/disable function. 15.2.3 general timer/counter1 control register - gtc cr ? bit 2- foc1a: force output compare match 1a writing a logical one to this bit forces a change i n the compare match output pin pb1 (oc1a) according to the values already set in com1a1 and c om1a0. if com1a1 and com1a0 written in the same cycle as foc1a, the new settings will b e used. the force output compare bit can be used to change the output pin value regardless o f the timer value. the automatic action pro- grammed in com1a1 and com1a0 takes place as if a co mpare match had occurred, but no interrupt is generated. the foc1a bit always reads as zero. foc1a is not in use if pwm1a bit is set. ? bit 1- psr1 : prescaler reset timer/counter1 when this bit is set (one), the timer/counter presc aler (tcnt1 is unaffected) will be reset. the bit will be cleared by hardware after the operation is performed. writing a zero to this bit will have no effect. this bit will always read as zero. 15.2.4 timer/counter1 - tcnt1 this 8-bit register contains the value of timer/cou nter1. timer/counter1 is realized as an up counter with re ad and write access. due to synchronization of the cpu, timer/counter1 data written into timer/ counter1 is delayed by one cpu clock cycle in synchronous mode and at most two cpu clock cycle s for asynchronous mode. 0 1 1 1 ck/4 1 0 0 0 ck/8 1 0 0 1 ck/16 1 0 1 0 ck/32 1 0 1 1 ck/64 1 1 0 0 ck/128 1 1 0 1 ck/256 1 1 1 0 ck/512 1 1 1 1 ck/1024 table 15-2. timer/counter1 prescale select (continued) cs13 cs12 cs11 cs10 t/c1 clock bit 7 6 5 4 3 2 1 0 $2c ($4c) tsm pwm1b com1b1 com1b0 foc1b foc1a psr1 psr0 gtccr read/write r/w r/w r/w r/w w w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 $2f ($4f) msb lsb tcnt1 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
96 7598d?avr?02/07 attiny25/45/85 auto 15.2.5 timer/counter1 output compare registera - ocr 1a the output compare register a is an 8-bit read/writ e register. the timer/counter output compare register a contain s data to be continuously compared with timer/counter1. actions on compare matches are spec ified in tccr1. a compare match does only occur if timer/counter1 counts to the ocr1a va lue. a software write that sets tcnt1 and ocr1a to the same value does not generate a compare match. a compare match will set the compare interrupt flag ocf1a after a synchronization delay follow- ing the compare event. 15.2.6 timer/counter1 output compare register c - oc r1c the output compare register b - ocr1b from attiny15 is replaced with the output compare register c - ocr1c that is an 8-bit read/write regi ster. this register has the same function as the output compare register b in attiny15. the timer/counter output compare register c contain s data to be continuously compared with timer/counter1. a compare match does only occur if timer/counter1 counts to the ocr1c value. a software write that sets tcnt1 and ocr1c t o the same value does not generate a compare match. if the ctc1 bit in tccr1 is set, a c ompare match will clear tcnt1. ? bit 7 - res: reserved bit this bit is a reserved bit in the attiny25/45/85 an d always reads as zero. ? bit 6 - ocie1a: timer/counter1 output compare inte rrupt enable when the ocie1a bit is set (one) and the i-bit in t he status register is set (one), the timer/counter1 compare matcha, interrupt is enabled . the corresponding interrupt at vector $003 is executed if a compare matcha occurs. the co mpare flag in timer/counter1 is set (one) in the timer/counter interrupt flag register. ? bit 2 - toie1: timer/counter1 overflow interrupt e nable when the toie1 bit is set (one) and the i-bit in th e status register is set (one), the timer/counter1 overflow interrupt is enabled. the c orresponding interrupt (at vector $004) is executed if an overflow in timer/counter1 occurs. t he overflow flag (timer1) is set (one) in the timer/counter interrupt flag register - tifr. ? bit 0 - res: reserved bit bit 7 6 5 4 3 2 1 0 $2e ($4e) msb lsb ocr1a read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 $2b ($4b) msb lsb ocr1c read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 1 1 1 1 1 1 1 1 bit 7 6 5 4 3 2 1 0 $39 ($59) - ocie1a ocie1b ocie0a ocie0b toie1 toie0 - timsk read/write r r/w r/w r r r/w r/w r initial value 0 0 0 0 0 0 0 0
97 7598d?avr?02/07 attiny25/45/85 auto this bit is a reserved bit in the attiny25/45/85 an d always reads as zero. 15.2.7 timer/counter interrupt flag register - tifr ? bit 7 - res: reserved bit this bit is a reserved bit in the attiny25/45/85 an d always reads as zero. ? bit 6 - ocf1a: output compare flag 1a the ocf1a bit is set (one) when compare match occur s between timer/counter1 and the data value in ocr1a - output compare register 1a. ocf1a is cleared by hardware when executing the corresponding interrupt handling vector. altern atively, ocf1a is cleared, after synchroniza- tion clock cycle, by writing a logic one to the fla g. when the i-bit in sreg, ocie1a, and ocf1a are set (one), the timer/counter1 a compare match i nterrupt is executed. ? bit 2 - tov1: timer/counter1 overflow flag the bit tov1 is set (one) when an overflow occurs i n timer/counter1. tov1 is cleared by hard- ware when executing the corresponding interrupt han dling vector. alternatively, tov1 is cleared, after synchronization clock cycle, by writ ing a logical one to the flag. when the sreg i- bit, and toie1 (timer/counter1 overflow interrupt e nable), and tov1 are set (one), the timer/counter1 overflow interrupt is executed. ? bit 0 - res: reserved bit this bit is a reserved bit in the attiny25/45/85 an d always reads as zero. 15.2.8 pll control and status register - pllcsr ? bit 7.. 3- res : reserved bits these bits are reserved bits in the attiny25/45/85 and always read as zero. ? bit 2- pcke: pck enable the bit pcke is always set in the attiny15 compatib ility mode. ? bit 1- plle: pll enable the pll is always enabled in the attiny15 compatibi lity mode. ? bit 0- plock: pll lock detector when the plock bit is set, the pll is locked to the reference clock. bit 7 6 5 4 3 2 1 0 $38 ($58) - ocf1a ocf1b ocf0a ocf0b tov1 tov0 - tifr read/write r r/w r/w r r r/w r/w r initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 $27 ($27) lsm - - - - pcke plle plock pllcsr read/write r r r r r r/w r/w r initial value 0 0 0 0 0 0 0/1 0
98 7598d?avr?02/07 attiny25/45/85 auto 15.2.9 timer/counter1 in pwm mode when the pwm mode is selected, timer/counter1 and t he output compare register a - ocr1a form an 8-bit, free-running and glitch-free p wm generator with output on the pb1(oc1a). when the counter value match the content of ocr1a, the oc1a and output is set or cleared according to the com1a1/com1a0 bits in the timer/co unter1 control register a - tccr1, as shown in table 15-3 . timer/counter1 acts as an up-counter, counting from $00 up to the value specified in the output compare register ocr1c, and starting from $00 up ag ain. a compare match with ocr1c will set an overflow interrupt flag (tov1) after a synch ronization delay following the compare event. note that in pwm mode, writing to the output compar e register ocr1a, the data value is first transferred to a temporary location. the value is l atched into ocr1a when the timer/counter reaches ocr1c. this prevents the occurrence of odd- length pwm pulses (glitches) in the event of an unsynchronized ocr1a. see figure 15-4 for an e xample. figure 15-4. effects of unsynchronized ocr latching during the time between the write and the latch ope ration, a read from ocr1a will read the con- tents of the temporary location. this means that th e most recently written value always will read out of ocr1a. table 15-3. compare mode select in pwm mode com1a1 com1a0 effect on output compare pin 0 0 oc1a not connected. 0 1 oc1a not connected. 1 0 oc1a cleared on compare match. set when tcnt1 = $ 01. 1 1 oc1a set on compare match. cleared when tcnt1 = $ 01. pwm output oc1apwm output oc1a unsynchronized oc1a latch synchronized oc1a latch counter valuecompare value counter value compare value compare value changes glitch compare value changes
99 7598d?avr?02/07 attiny25/45/85 auto when ocr1a contains $00 or the top value, as specif ied in ocr1c register, the output pb1(oc1a) is held low or high according to the sett ings of com1a1/com1a0. this is shown in table 15-4 . in pwm mode, the timer overflow flag - tov1 is set when the tcnt1 counts to the ocr1c value and the tcnt1 is reset to $00. the timer over flow interrupt1 is executed when tov1 is set provided that timer overflow interrupt and glob al interrupts are enabled. this also applies to the timer output compare flags and interrupts. the frequency of the pwm will be timer clock 1 freq uency divided by (ocr1c value + 1). see the following equation: resolution shows how many bit is required to expres s the value in the ocr1c register. it is cal- culated by following equation resolution pwm = log 2 (ocr1c + 1). table 15-4. pwm outputs ocr1a = $00 or ocr1c com1a1 com1a0 ocr1a output oc1a 0 1 $00 l 0 1 ocr1c h 1 0 $00 l 1 0 ocr1c h 1 1 $00 h 1 1 ocr1c l table 15-5. timer/counter1 clock prescale select in the asynchr onous mode pwm frequency clock selection cs13..cs10 ocr1c resoluti on 20 khz pck/16 0101 199 7.6 30 khz pck/16 0101 132 7.1 40 khz pck/8 0100 199 7.6 50 khz pck/8 0100 159 7.3 60 khz pck/8 0100 132 7.1 70 khz pck/4 0011 228 7.8 80 khz pck/4 0011 199 7.6 90 khz pck/4 0011 177 7.5 100 khz pck/4 0011 159 7.3 110 khz pck/4 0011 144 7.2 f pwm f tck1 ocr1c + 1 ( ) ------------------------------------ =
100 7598d?avr?02/07 attiny25/45/85 auto 16. dead time generator the dead time generator is provided for the timer/c ounter1 pwm output pairs to allow driving external power control switches safely. the dead ti me generator is a separate block that can be connected to timer/counter1 and it is used to in sert dead times (non-overlapping times) for the timer/counter1 complementary output pairs (oc1a -oc1a and oc1b-oc1b ). the sharing of tasks is as follows: the timer/counter generates the pwm output and the dead time genera- tor generates the non-overlapping pwm output pair f rom the timer/counter pwm signal. two dead time generators are provided, one for each pwm output. the non-overlap time is adjust- able and the pwm output and it?s complementary outp ut are adjusted separately, and independently for both pwm outputs. figure 16-1. timer/counter1 & dead time generators 120 khz pck/4 0011 132 7.1 130 khz pck/2 0010 245 7.9 140 khz pck/2 0010 228 7.8 150 khz pck/2 0010 212 7.7 160 khz pck/2 0010 199 7.6 170 khz pck/2 0010 187 7.6 180 khz pck/2 0010 177 7.5 190 khz pck/2 0010 167 7.4 200 khz pck/2 0010 159 7.3 250 khz pck 0001 255 8.0 300 khz pck 0001 212 7.7 350 khz pck 0001 182 7.5 400 khz pck 0001 159 7.3 450 khz pck 0001 141 7.1 500 khz pck 0001 127 7.0 table 15-5. timer/counter1 clock prescale select in the asynchr onous mode pwm frequency clock selection cs13..cs10 ocr1c resoluti on timer/counter1 oc1a oc1a oc1b oc1b dead time generator pwm generator pcke t15m pck ck dt1ah dt1bh dead time generator pwm1b pwm1a dt1al dt1bl
101 7598d?avr?02/07 attiny25/45/85 auto the dead time generation is based on the 4-bit down counters that count the dead time, as shown in figure 46. there is a dedicated prescaler in front of the dead time generator that can divide the timer/counter1 clock (pck or ck) by 1, 2 , 4 or 8. this provides for large range of dead times that can be generated. the prescaler is controlled by two control bits dtps11..10 from the i/o register at address 0x23. the block ha s also a rising and falling edge detector that is used to start the dead time counting period. dep ending on the edge, one of the transitions on the rising edges, oc1x or oc1x is delayed until the counter has counted to zero. the compara- tor is used to compare the counter with zero and st op the dead time insertion when zero has been reached. the counter is loaded with a 4-bit dt 1xh or dt1xl value from dt1x i/o register, depending on the edge of the pwm generator output w hen the dead time insertion is started. figure 16-2. dead time generator the length of the counting period is user adjustabl e by selecting the dead time prescaler setting in 0x23 register, and selecting then the dead time value in i/o register dt1x. the dt1x register consists of two 4-bit fields, dt1xh and dt1xl that control the dead time periods of the pwm output and its? complementary output separately. th us the rising edge of oc1x and oc1x can have different dead time periods. the dead time is adjusted as the number of prescaled dead time generator clock cycles. figure 16-3. the complementary output pair 16.0.1 timer/counter1 dead time prescaler register 1 - dtps1 clock control oc1x oc1x t/c1 clock pwm1x 4-bit counter comparator dt1xl dt1xh dt1x i/o register dead time prescaler dtps11..10 oc1xx = a or b t non-overlap / rising edge t non-overlap / falling edge oc1x pwm1x bit 7 6 5 4 3 2 1 0 $23 ($43) dtps11 dtps10 dtps1 read/write r r r r r r r/w r/w initial value 0 0 0 0 0 0 0 0
102 7598d?avr?02/07 attiny25/45/85 auto the dead time prescaler register, dtps1 is a 2-bit read/write register. the dedicated dead time prescaler in front of the d ead time generator can divide the timer/counter1 clock (pck or ck) by 1, 2, 4 or 8 pr oviding a large range of dead times that can be generated. the dead time prescaler is controlled by two bits dtps11..10 from the dead time prescaler register. these bits define the divi sion factor of the dead time prescaler. the division factors are given in table 46.. 16.0.2 timer/counter1 dead time a - dt1a the dead time value register a is an 8-bit read/wri te register. the dead time delay of is adjusted by the dead time value register, dt1a. the register consists of two fields, dt1ah3..0 and dt1al3..0, one for eac h complementary output. therefore a differ- ent dead time delay can be adjusted for the rising edge of oc1a and the rising edge of oc1a . ? bits 7..4- dt1ah3..dt1ah0: dead time value for oc1 a output the dead time value for the oc1a output. the dead t ime delay is set as a number of the pres- caled timer/counter clocks. the minimum dead time i s zero and the maximum dead time is the prescaled time/counter clock period multiplied by 1 5. ? bits 3..0- dt1al3..dt1al0: dead time value for oc1 a output the dead time value for the oc1a output. the dead time delay is set as a number of the pres- caled timer/counter clocks. the minimum dead time i s zero and the maximum dead time is the prescaled time/counter clock period multiplied by 1 5. 16.0.3 timer/counter1 dead time b - dt1b the dead time value register bis an 8-bit read/writ e register. the dead time delay of is adjusted by the dead time value register, dt1b. the register consists of two fields, dt1bh3..0 and dt1bl3..0, one for eac h complementary output. therefore a differ- ent dead time delay can be adjusted for the rising edge of oc1a and the rising edge of oc1a . ? bits 7..4- dt1bh3..dt1bh0: dead time value for oc1 b output table 16-1. division factors of the dead time prescaler dtps11 dtps10 prescaler divides the t/c1 clock by 0 0 1x (no division) 0 1 2x 1 0 4x 1 1 8x bit 7 6 5 4 3 2 1 0 $25 ($45) dt1ah3 dt1ah2 dt1ah1 dt1ah0 dt1al3 dt1al2 dt1al1 dt1al0 dt1 a read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 $25 ($45) dt1bh3 dt1bh2 dt1bh1 dt1bh0 dt1bl3 dt1bl2 dt1bl1 dt1bl0 dt1 b read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
103 7598d?avr?02/07 attiny25/45/85 auto the dead time value for the oc1b output. the dead t ime delay is set as a number of the pres- caled timer/counter clocks. the minimum dead time i s zero and the maximum dead time is the prescaled time/counter clock period multiplied by 1 5. ? bits 3..0- dt1bl3..dt1bl0: dead time value for oc1 b output the dead time value for the oc1b output. the dead time delay is set as a number of the pres- caled timer/counter clocks. the minimum dead time i s zero and the maximum dead time is the prescaled time/counter clock period multiplied by 1 5. 17. universal serial interface ? usi the universal serial interface, or usi, provides th e basic hardware resources needed for serial communication. combined with a minimum of control s oftware, the usi allows significantly higher transfer rates and uses less code space than solutions based on software only. interrupts are included to minimize the processor load. the ma in features of the usi are: ? two-wire synchronous data transfer (master or slave , f sclmax = f ck /16) ? three-wire synchronous data transfer (master or sla ve f sckmax = f ck /4) ? data received interrupt ? wakeup from idle mode ? in two-wire mode: wake-up from all sleep modes, inc luding power-down mode ? two-wire start condition detector with interrupt ca pability 17.1 overview a simplified block diagram of the usi is shown on f igure 17-1. for the actual placement of i/o pins, refer to ?pinout attiny25/45/85? on page 2 . cpu accessible i/o registers, including i/o bits and i/o pins, are shown in bold. the device-sp ecific i/o register and bit locations are listed in the ?usi register descriptions? on page 110 . figure 17-1. universal serial interface, block diagram data bus usipf usitc usiclk usics0 usics1 usioif usioie usidc usisif usiwm0 usiwm1 usisie bit7 two-wire clock control unit do (output only) di/sda (input/open drain) usck/scl (input/open drain) 4-bit counter usidr usisr d q le usicr clock hold tim0 comp bit0 [1] 30 1 2 30 1 2 01 2 usidb
104 7598d?avr?02/07 attiny25/45/85 auto the 8-bit shift register is directly accessible via the data bus and contains the incoming and outgoing data. the register has no buffering so the data must be read as quickly as possible to ensure that no data is lost. the most significant b it is connected to one of two output pins depending of the wire mode configuration. a transpa rent latch is inserted between the serial register output and output pin, which delays the ch ange of data output to the opposite clock edge of the data input sampling. the serial input i s always sampled from the data input (di) pin independent of the configuration. the 4-bit counter can be both read and written via the data bus, and can generate an overflow interrupt. both the serial register and the counter are clocked simultaneously by the same clock source. this allows the counter to count the number of bits received or transmitted and generate an interrupt when the transfer is complete. note th at when an external clock source is selected the counter counts both clock edges. in this case t he counter counts the number of edges, and not the number of bits. the clock can be selected f rom three different sources: the usck pin, timer/counter0 compare match or from software. the two-wire clock control unit can generate an int errupt when a start condition is detected on the two-wire bus. it can also generate wait states by holding the clock pin low after a start con- dition is detected, or after the counter overflows. 17.2 functional descriptions 17.2.1 three-wire mode the usi three-wire mode is compliant to the serial peripheral interface (spi) mode 0 and 1, but does not have the slave select (ss) pin functionality. however, this feature can b e implemented in software if necessary. pin names used by this mo de are: di, do, and usck. figure 17-2. three-wire mode operation, simplified diagram figure 17-2 shows two usi units operating in three-wire mode, one as master and one as slave. the two shift registers are interconnected i n such way that after eight usck clocks, the slave master bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 do di usck bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 do di usck portxn
105 7598d?avr?02/07 attiny25/45/85 auto data in each register are interchanged. the same cl ock also increments the usi?s 4-bit counter. the counter overflow (interrupt) flag, or usioif, c an therefore be used to determine when a transfer is completed. the clock is generated by th e master device software by toggling the usck pin via the port register or by writing a one to the usitc bit in usicr. figure 17-3. three-wire mode, timing diagram the three-wire mode timing is shown in figure 17-3. at the top of the figure is a usck cycle ref- erence. one bit is shifted into the usi shift regis ter (usidr) for each of these cycles. the usck timing is shown for both external clock modes. in external clock mode 0 (usics0 = 0), di is sampled at positive edges, and do is changed (da ta register is shifted by one) at negative edges. external clock mode 1 (usics0 = 1) uses the opposite edges versus mode 0, i.e., sam- ples data at negative and changes the output at pos itive edges. the usi clock modes corresponds to the spi data mode 0 and 1. referring to the timing diagram (figure 17-3.), a b us transfer involves the following steps: 1. the slave device and master device sets up its da ta output and, depending on the pro- tocol used, enables its output driver (mark a and b ). the output is set up by writing the data to be transmitted to the serial data register. enabling of the output is done by set- ting the corresponding bit in the port data directi on register. note that point a and b does not have any specific order, but both must be at least one half usck cycle before point c where the data is sampled. this must be don e to ensure that the data setup requirement is satisfied. the 4-bit counter is rese t to zero. 2. the master generates a clock pulse by software to ggling the usck line twice (c and d). the bit value on the slave and master?s data in put (di) pin is sampled by the usi on the first edge (c), and the data output is changed on the opposite edge (d). the 4-bit counter will count both edges. 3. step 2. is repeated eight times for a complete re gister (byte) transfer. 4. after eight clock pulses (i.e., 16 clock edges) t he counter will overflow and indicate that the transfer is completed. the data bytes transferr ed must now be processed before a new transfer can be initiated. the overflow interru pt will wake up the processor if it is set to idle mode. depending of the protocol used th e slave device can now set its out- put to high impedance. 17.2.2 spi master operation example the following code demonstrates how to use the usi module as a spi master: spitransfer: sts usidr,r16 ldi r16,(1< 106 7598d?avr?02/07 attiny25/45/85 auto spitransfer_loop: sts usicr,r16 lds r16, usisr sbrs r16, usioif rjmp spitransfer_loop lds r16,usidr ret the code is size optimized using only eight instruc tions (+ ret). the code example assumes that the do and usck pins are enabled as output in the d dre register. the value stored in register r16 prior to the function is called is transferred to the slave device, and when the transfer is com- pleted the data received from the slave is stored b ack into the r16 register. the second and third instructions clears the usi co unter overflow flag and the usi counter value. the fourth and fifth instruction set three-w ire mode, positive edge shift register clock, count at usitc strobe, and toggle usck. the loop is repeated 16 times. the following code demonstrates how to use the usi module as a spi master with maximum speed (fsck = fck/4): spitransfer_fast: sts usidr,r16 ldi r16,(1< 107 7598d?avr?02/07 attiny25/45/85 auto ldi r16,(1< 108 7598d?avr?02/07 attiny25/45/85 auto figure 17-4. two-wire mode operation, simplified diagram figure 17-4 shows two usi units operating in two-wire mode, on e as master and one as slave. it is only the physical layer that is shown since t he system operation is highly dependent of the communication scheme used. the main differences bet ween the master and slave operation at this level, is the serial clock generation which is always done by the master, and only the slave uses the clock control unit. clock generation must be implemented in software, but the shift operation is done automatically by both devices. no te that only clocking on negative edge for shifting data is of practical use in this mode. the slave can insert wait states at start or end of transfer by forcing the scl clock low. this means t hat the master must always check if the scl line was actually released after it has generated a positive edge. since the clock also increments the counter, a coun ter overflow can be used to indicate that the transfer is completed. the clock is generated by th e master by toggling the usck pin via the port register. the data direction is not given by the physical lay er. a protocol, like the one used by the twi- bus, must be implemented to control the data flow. figure 17-5. two-wire mode, typical timing diagram referring to the timing diagram (figure 17-5.), a b us transfer involves the following steps: master slave bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 sdascl bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 two-wire clock control unit hold scl portxn sdascl vcc p s address 1 - 7 8 9 r/w ack ack 1 - 8 9 data ack 1 - 8 9 data sda scl a b d e c f
109 7598d?avr?02/07 attiny25/45/85 auto 1. the a start condition is generated by the master by forcing the sda low line while the scl line is high (a). sda can be forced low either by writing a zero to bit 7 of the shift register, or by setting the corresponding bit in th e port register to zero. note that the data direction register bit must be set to one for the output to be enabled. the slave device?s start detector logic (figure 17-6.) detect s the start condition and sets the usisif flag. the flag can generate an interrupt if necessary. 2. in addition, the start detector will hold the scl line low after the master has forced an negative edge on this line (b). this allows the sla ve to wake up from sleep or complete its other tasks before setting up the shift registe r to receive the address. this is done by clearing the start condition flag and reset the counter. 3. the master set the first bit to be transferred an d releases the scl line (c). the slave samples the data and shift it into the serial regis ter at the positive edge of the scl clock. 4. after eight bits are transferred containing slave address and data direction (read or write), the slave counter overflows and the scl lin e is forced low (d). if the slave is not the one the master has addressed, it releases the s cl line and waits for a new start condition. 5. if the slave is addressed it holds the sda line l ow during the acknowledgment cycle before holding the scl line low again (i.e., the co unter register must be set to 14 before releasing scl at (d)). depending of the r/w bit the master or slave enables its output. if the bit is set, a master read operation is in progress (i.e., the slave drives the sda line) the slave can hold the scl line low after the acknowledge (e). 6. multiple bytes can now be transmitted, all in sam e direction, until a stop condition is given by the master (f). or a new start condition i s given. if the slave is not able to receive more data it do es not acknowledge the data byte it has last received. when the master does a read operation it must terminate the operation by force the acknowledge bit low after the last byte transmitted . figure 17-6. start condition detector, logic diagram 17.2.5 start condition detector the start condition detector is shown in figure 17- 6. the sda line is delayed (in the range of 50 to 300 ns) to ensure valid sampling of the scl line . the start condition detector is only enabled in two-wire mode. the start condition detector is working asynchronou sly and can therefore wake up the processor from the power-down sleep mode. however, the protoc ol used might have restrictions on the scl hold time. therefore, when using this feature i n this case the oscillator start-up time set by the cksel fuses (see ?clock systems and their distribution? on page 21 ) must also be taken into the consideration. refer to the usisif bit des cription on page 111 for further details. 17.3 alternative usi usage when the usi unit is not used for serial communicat ion, it can be set up to do alternative tasks due to its flexible design. sda scl write( usisif) clockhold usisif d q clr d q clr
110 7598d?avr?02/07 attiny25/45/85 auto 17.3.1 half-duplex asynchronous data transfer by utilizing the shift register in three-wire mode, it is possible to implement a more compact and higher performance uart than by software only. 17.3.2 4-bit counter the 4-bit counter can be used as a stand-alone coun ter with overflow interrupt. note that if the counter is clocked externally, both clock edges wil l generate an increment. 17.3.3 12-bit timer/counter combining the usi 4-bit counter and timer/counter0 allows them to be used as a 12-bit counter. 17.3.4 edge triggered external interrupt by setting the counter to maximum value (f) it can function as an additional external interrupt. the overflow flag and interrupt enable bit are then used for the external interrupt. this feature is selected by the usics1 bit. 17.3.5 software interrupt the counter overflow interrupt can be used as a sof tware interrupt triggered by a clock strobe. 17.4 usi register descriptions 17.4.1 usi data register ? usidr when accessing the usi data register (usidr) the se rial register can be accessed directly. if a serial clock occurs at the same cycle the registe r is written, the register will contain the value written and no shift is performed. a (left) shift o peration is performed depending of the usics1..0 bits setting. the shift operation can be controlled by an external clock edge, by a timer/counter0 compare match, or directly by softwa re using the usiclk strobe bit. note that even when no wire mode is selected (usiwm1..0 = 0) both the external data input (di/sda) and the external clock input (usck/scl) can still be us ed by the shift register. the output pin in use, do or sda depending on the w ire mode, is connected via the output latch to the most significant bit (bit 7) of the data reg ister. the output latch is open (transparent) dur- ing the first half of a serial clock cycle when an external clock source is selected (usics1 = 1), and constantly open when an internal clock source i s used (usics1 = 0). the output will be changed immediately when a new msb written as long as the latch is open. the latch ensures that data input is sampled and data output is chang ed on opposite clock edges. note that the corresponding data direction register to the pin must be set to one for enabling data output from the shift register. 17.4.2 usi buffer register ? usibr bit 7 6 5 4 3 2 1 0 msb lsb usidr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 msb lsb usibr read/write r r r r r r r r initial value 0 0 0 0 0 0 0 0
111 7598d?avr?02/07 attiny25/45/85 auto the content of the serial register is loaded to the usi buffer register when the trasfer is com- pleted, and instead of accessing the usi data regis ter (the serial register) the usi data buffer can be accessed when the cpu reads the received dat a. this gives the cpu time to handle other program tasks too as the controlling of the u si is not so timing critical. the usi flags as set same as when reading the usidr register. 17.4.3 usi status register ? usisr the status register contains interrupt flags, line status flags and the counter value. ? bit 7 ? usisif: start condition interrupt flag when two-wire mode is selected, the usisif flag is set (to one) when a start condition is detected. when output disable mode or three-wire mo de is selected and (usicsx = 0b11 & usiclk = 0) or (usics = 0b10 & usiclk = 0), any edg e on the sck pin sets the flag. an interrupt will be generated when the flag is set while the usisie bit in usicr and the global interrupt enable flag are set. the flag will only b e cleared by writing a logical one to the usisif bit. clearing this bit will release the start detec tion hold of uscl in two-wire mode. a start condition interrupt will wakeup the process or from all sleep modes. ? bit 6 ? usioif: counter overflow interrupt flag this flag is set (one) when the 4-bit counter overf lows (i.e., at the transition from 15 to 0). an interrupt will be generated when the flag is set wh ile the usioie bit in usicr and the global interrupt enable flag are set. the flag will only b e cleared if a one is written to the usioif bit. clearing this bit will release the counter overflow hold of scl in two-wire mode. a counter overflow interrupt will wakeup the proces sor from idle sleep mode. ? bit 5 ? usipf: stop condition flag when two-wire mode is selected, the usipf flag is s et (one) when a stop condition is detected. the flag is cleared by writing a one to this bit. n ote that this is not an interrupt flag. this signal is useful when implementing two-wire bus master arbitr ation. ? bit 4 ? usidc: data output collision this bit is logical one when bit 7 in the shift reg ister differs from the physical pin value. the flag is only valid when two-wire mode is used. this sign al is useful when implementing two-wire bus master arbitration. ? bits 3..0 ? usicnt3..0: counter value these bits reflect the current 4-bit counter value. the 4-bit counter value can directly be read or written by the cpu. the 4-bit counter increments by one for each clock generated either by the external clock edge detector, by a timer/counter0 compare match, or by software using usiclk or usitc strobe bits. the clock source depends of the setting of th e usics1..0 bits. for external clock operation a special feature is added that allows the clock to be generated by writing to the usitc strobe bit 7 6 5 4 3 2 1 0 usisif usioif usipf usidc usicnt3 usicnt2 usicnt1 usicnt0 u sisr read/write r/w r/w r/w r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
112 7598d?avr?02/07 attiny25/45/85 auto bit. this feature is enabled by write a one to the usiclk bit while setting an external clock source (usics1 = 1). note that even when no wire mode is selected (usiwm 1..0 = 0) the external clock input (usck/scl) are can still be used by the counter. 17.4.4 usi control register ? usicr the control register includes interrupt enable cont rol, wire mode setting, clock select setting, and clock strobe. ? bit 7 ? usisie: start condition interrupt enable setting this bit to one enables the start condition detector interrupt. if there is a pending inter- rupt when the usisie and the global interrupt enabl e flag is set to one, this will immediately be executed. refer to the usisif bit description on pa ge 111 for further details. ? bit 6 ? usioie: counter overflow interrupt enable setting this bit to one enables the counter overflo w interrupt. if there is a pending interrupt when the usioie and the global interrupt enable flag is set to one, this will immediately be executed. refer to the usioif bit description on page 111 for further details. ? bit 5..4 ? usiwm1..0: wire mode these bits set the type of wire mode to be used. ba sically only the function of the outputs are affected by these bits. data and clock inputs are n ot affected by the mode selected and will always have the same function. the counter and shif t register can therefore be clocked exter- nally, and data input sampled, even when outputs ar e disabled. the relations between usiwm1..0 and the usi operation is summarized in table 17-1 . bit 7 6 5 4 3 2 1 0 usisie usioie usiwm1 usiwm0 usics1 usics0 usiclk usitc usic r read/write r/w r/w r/w r/w r/w r/w w w initial value 0 0 0 0 0 0 0 0
113 7598d?avr?02/07 attiny25/45/85 auto note: 1. the di and usck pins are renamed to serial data (sda) and serial clock (scl) respectively to avoid confusion between the modes of operation. ? bit 3..2 ? usics1..0: clock source select these bits set the clock source for the shift regis ter and counter. the data output latch ensures that the output is changed at the opposite edge of the sampling of the data input (di/sda) when using external clock source (usck/scl). when softwa re strobe or timer/counter0 compare match clock option is selected, the output latch is transparent and therefore the output is changed immediately. clearing the usics1..0 bits en ables software strobe option. when using this option, writing a one to the usiclk bit clocks both the shift register and the counter. for external clock source (usics1 = 1), the usiclk bit is no longer used as a strobe, but selects between external clocking and software clocking by the usitc strobe bit. table 17-1. relations between usiwm1..0 and the usi operation usiwm1 usiwm0 description 0 0 outputs, clock hold, and start detector disabled. p ort pins operates as normal. 0 1 three-wire mode. uses do, di, and usck pins. the data output (do) pin overrides the corresponding bit in the po rt register in this mode. however, the corresponding d dr bit still controls the data direction. when the port pin is set as input t he pins pull-up is controlled by the port bit. the data input (di) and serial clock (usck) pins do not affect the normal port operation. when operating as master, clock pul ses are software generated by toggling the port register, while the data direction is set to output. the usitc bit in the usicr register can be used for this purpose. 1 0 two-wire mode. uses sda (di) and scl (usck) pins (1) . the serial data (sda) and the serial clock (scl) pins are bi-directional and uses open-collector output drives. the output drive rs are enabled by setting the corresponding bit for sda and scl in the ddr re gister. when the output driver is enabled for the sda pin, the output driver will force the line sda low if the output of the shift registe r or the corresponding bit in the port register is zero. otherwise the sda line w ill not be driven (i.e., it is released). when the scl pin output driver is enable d the scl line will be forced low if the corresponding bit in the port reg ister is zero, or by the start detector. otherwise the scl line will not be driven. the scl line is held low when a start detector dete cts a start condition and the output is enabled. clearing the start condition flag (usisif) releases the line. the sda and scl pin inputs is not affected by enabling this mode. pull- ups on the sda and scl port pin are disabled in two -wire mode. 1 1 two-wire mode. uses sda and scl pins. same operation as for the two-wire mode described a bove, except that the scl line is also held low when a counter overflow o ccurs, and is held low until the counter overflow flag (usioif) is cleared.
114 7598d?avr?02/07 attiny25/45/85 auto table 17-2 shows the relationship between the usics1..0 and u siclk setting and clock source used for the shift register and the 4-bit counter. ? bit 1 ? usiclk: clock strobe writing a one to this bit location strobes the shif t register to shift one step and the counter to increment by one, provided that the usics1..0 bits are set to zero and by doing so the software clock strobe option is selected. the output will ch ange immediately when the clock strobe is exe- cuted, i.e., in the same instruction cycle. the val ue shifted into the shift register is sampled the previous instruction cycle. the bit will be read as zero. when an external clock source is selected (usics1 = 1), the usiclk function is changed from a clock strobe to a clock select register. setting the usiclk bit in this case will select the usitc strobe bit as clock source for the 4-bit coun ter (see table 17-2 ). ? bit 0 ? usitc: toggle clock port pin writing a one to this bit location toggles the usck /scl value either from 0 to 1, or from 1 to 0. the toggling is independent of the setting in the d ata direction register, but if the port value is to be shown on the pin the ddre4 must be set as out put (to one). this feature allows easy clock generation when implementing master devices. the bi t will be read as zero. when an external clock source is selected (usics1 = 1) and the usiclk bit is set to one, writ- ing to the usitc strobe bit will directly clock the 4-bit counter. this allows an early detection of when the transfer is done when operating as a maste r device. 18. analog comparator the analog comparator compares the input values on the positive pin ain0 and negative pin ain1. when the voltage on the positive pin ain0 is higher than the voltage on the negative pin ain1, the analog comparator output, aco, is set. th e comparator can trigger a separate inter- rupt, exclusive to the analog comparator. the user can select interrupt triggering on comparator output rise, fall or toggle. a block diagram of the comparator and its surrounding logic is shown in figure 18-1 . table 17-2. relations between the usics1..0 and usiclk setting usics1 usics0 usiclk shift register clock source 4-bit counter clock source 0 0 0 no clock no clock 0 0 1 software clock strobe (usiclk) software clock strobe (usiclk) 0 1 x timer/counter0 compare match timer/counter0 compare match 1 0 0 external, positive edge external, both edges 1 1 0 external, negative edge external, both edges 1 0 1 external, positive edge software clock strobe (us itc) 1 1 1 external, negative edge software clock strobe (us itc)
115 7598d?avr?02/07 attiny25/45/85 auto figure 18-1. analog comparator block diagram (2) notes: 1. see table 18-2 on page 116 . 2. refer to figure 1-1 on page 2 and table 10-5 on page 57 for analog comparator pin placement. 18.0.1 adc control and status register b ? adcsrb ? bit 6 ? acme: analog comparator multiplexer enable when this bit is written logic one and the adc is s witched off (aden in adcsra is zero), the adc multiplexer selects the negative input to the a nalog comparator. when this bit is written logic zero, ain1 is applied to the negative input o f the analog comparator. for a detailed description of this bit, see ?analog comparator multiplexed input? on page 116 . 18.0.2 analog comparator control and status register ? acsr ? bit 7 ? acd: analog comparator disable when this bit is written logic one, the power to th e analog comparator is switched off. this bit can be set at any time to turn off the analog compa rator. this will reduce power consumption in active and idle mode. when changing the acd bit, th e analog comparator interrupt must be disabled by clearing the acie bit in acsr. otherwis e an interrupt can occur when the bit is changed. ? bit 6 ? acbg: analog comparator bandgap select when this bit is set an internal 1.1v / 2.56v refer ence voltage replaces the positive input to the analog comparator. the selection of the internal vo ltage reference is done by writing the refs2..0 bits in admux register. when this bit is c leared, ain0 is applied to the positive input of the analog comparator. acbg bandgap reference adc multiplexer output acme aden (1) bit 7 6 5 4 3 2 1 0 bin acme ipr ? ? adts2 adts1 adts0 adcsrb read/write r r/w r r r r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 acd acbg aco aci acie ? acis1 acis0 acsr read/write r/w r/w r r/w r/w r r/w r/w initial value 0 0 n/a 0 0 0 0 0
116 7598d?avr?02/07 attiny25/45/85 auto ? bit 5 ? aco: analog comparator output the output of the analog comparator is synchronized and then directly connected to aco. the synchronization introduces a delay of 1 - 2 clock c ycles. ? bit 4 ? aci: analog comparator interrupt flag this bit is set by hardware when a comparator outpu t event triggers the interrupt mode defined by acis1 and acis0. the analog comparator interrupt routine is executed if the acie bit is set and the i-bit in sreg is set. aci is cleared by har dware when executing the corresponding inter- rupt handling vector. alternatively, aci is cleared by writing a logic one to the flag. ? bit 3 ? acie: analog comparator interrupt enable when the acie bit is written logic one and the i-bi t in the status register is set, the analog com- parator interrupt is activated. when written logic zero, the interrupt is disabled. ? bit 2 ? res: reserved bit this bit is a reserved bit in the attiny25/45/85 an d will always read as zero. ? bits 1, 0 ? acis1, acis0: analog comparator interr upt mode select these bits determine which comparator events that t rigger the analog comparator interrupt. the different settings are shown in table 18-1 . when changing the acis1/acis0 bits, the analog comp arator interrupt must be disabled by clearing its interrupt enable bit in the acsr regis ter. otherwise an interrupt can occur when the bits are changed. 18.1 analog comparator multiplexed input it is possible to select any of the adc3..0 pins to replace the negative input to the analog com- parator. the adc multiplexer is used to select this input, and consequently, the adc must be switched off to utilize this feature. if the analog comparator multiplexer enable bit (acme in adcsrb) is set and the adc is switched off (aden in adcsra is zero), mux1..0 in admux select the input pin to replace the negative input to the analog comparator, as shown in table 18-2 . if acme is cleared or aden is set, ain1 is applie d to the negative input to the analog comparator. table 18-1. acis1/acis0 settings acis1 acis0 interrupt mode 0 0 comparator interrupt on output toggle. 0 1 reserved 1 0 comparator interrupt on falling output edge. 1 1 comparator interrupt on rising output edge. table 18-2. analog comparator multiplexed input acme aden mux1..0 analog comparator negative input 0 x xx ain1 1 1 xx ain1 1 0 00 adc0
117 7598d?avr?02/07 attiny25/45/85 auto 18.1.1 digital input disable register 0 ? didr0 ? bits 1, 0 ? ain1d, ain0d: ain1, ain0 digital input disable when this bit is written logic one, the digital inp ut buffer on the ain1/0 pin is disabled. the corre- sponding pin register bit will always read as zero when this bit is set. when an analog signal is applied to the ain1/0 pin and the digital input fro m this pin is not needed, this bit should be writ- ten logic one to reduce power consumption in the di gital input buffer. 19. analog to digital converter 19.1 features ? 10-bit resolution ? 0.5 lsb integral non-linearity ? 2 lsb absolute accuracy ? 65 - 260 s conversion time ? up to 15 ksps at maximum resolution ? four multiplexed single ended input channels ? two differential input channels with selectable gai n ? temperature sensor input channel ? optional left adjustment for adc result readout ? 0 - v cc adc input voltage range ? selectable 1.1v / 2.56v adc voltage reference ? free running or single conversion mode ? adc start conversion by auto triggering on interrup t sources ? interrupt on adc conversion complete ? sleep mode noise cancele ? unipolar / bibilar input mode ? input polarity reversal mode the attiny25/45/85 features a 10-bit successive app roximation adc. the adc is connected to a 4-channel analog multiplexer which allows one dif ferential voltage input and four single-ended voltage inputs constructed from the pins of port b. the differential input (pb3, pb4 or pb2, pb5) is equipped with a programmable gain stage, providi ng amplification step of 26 db (20x) on the differential input voltage before the a/d conversio n. the single-ended voltage inputs refer to 0v (gnd). the adc contains a sample and hold circuit which en sures that the input voltage to the adc is held at a constant level during conversion. a block diagram of the adc is shown in figure 19-1 . 1 0 01 adc1 1 0 10 adc2 1 0 11 adc3 table 18-2. analog comparator multiplexed input acme aden mux1..0 analog comparator negative input bit 7 6 5 4 3 2 1 0 ? ? adc0d adc2d adc3d adc1d ain1d ain0d didr0 read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
118 7598d?avr?02/07 attiny25/45/85 auto internal voltage references of nominally 1.1v or 2. 56v are provided on-chip and these voltage references can optionally be externally decoupled a t the aref (pb0) pin by a capacitor, for bet- ter noise performance. alternatively, v cc can be used as voltage reference for single ended channels. there is also an option to use an externa l voltage reference and turn-off the internal voltage reference. these options are selected using the refs2..0 bits of the admux control register. figure 19-1. analog to digital converter block schematic 19.2 operation the adc converts an analog input voltage to a 10-bi t digital value through successive approxi- mation. the minimum value represents gnd and the ma ximum value represents the voltage on v cc , the voltage on the aref pin or an internal 1.1v / 2.56v voltage reference. the voltage reference for the adc may be selected b y writing to the refs2..0 bits in admux. the vcc supply, the aref pin or an internal 1.1v / 2.56v voltage reference may be selected as the adc voltage reference. optionally the internal 1.1v / 2.56v voltage reference may be decou- pled by an external capacitor at the aref pin to im prove noise immunity. the analog input channel and differential gain are selected by writing to the mux3..0 bits in admux. any of the four adc input pins adc3..0 can b e selected as single ended inputs to the adc conversion complete irq 8-bit data bus 15 0 adc multiplexer select (admux) adc ctrl. & status a register (adcsra) adc data register (adch/adcl) adie adate adsc aden adif adif mux1 mux0 adps0 adps1 adps2 conversion logic 10-bit dac + - sample & holdcomparator internal 1.1v/2.56v reference mux decoder mux2 aref adc3adc2 adc1 adc0 refs2..0 adlar channel selection adc[9:0] adc multiplexer output prescaler input mux trigger select adts[2:0] interrupt flags start + - gain selection gain amplifier neg. input mux single ended / differential selection temperature sensor adc4 adc ctrl. & status b register (adcsrb) bin ipr v cc
119 7598d?avr?02/07 attiny25/45/85 auto adc. adc2 or adc0 can be selected as positive input and adc0, adc1, adc2 or adc3 can be selected as negative input to the differential g ain amplifier. if differential channels are selected, the differen tial gain stage amplifies the voltage difference between the selected input pair by the selected gai n factor, 1x or 20x, according to the setting of the mux3..0 bits in admux. this amplified value the n becomes the analog input to the adc. if single ended channels are used, the gain amplifier is bypassed altogether. if adc0 or adc2 is selected as both the positive an d negative input to the differential gain amplifier (adc0-adc0 or adc2-adc2), the remaining o ffset in the gain stage and conversion circuitry can be measured directly as the result of the conversion. this figure can be subtracted from subsequent conversions with the same gain sett ing to reduce offset error to below 1 lsw. the on-chip temperature sensor is selected by writi ng the code ?1111? to the mux3..0 bits in admux register when the adc4 channel is used as an adc input. the adc is enabled by setting the adc enable bit, a den in adcsra. voltage reference and input channel selections will not go into effect un til aden is set. the adc does not consume power when aden is cleared, so it is recommended to switch off the adc before entering power saving sleep modes. the adc generates a 10-bit result which is presente d in the adc data registers, adch and adcl. by default, the result is presented right adj usted, but can optionally be presented left adjusted by setting the adlar bit in admux. if the result is left adjusted and no more than 8-b it precision is required, it is sufficient to read adch. otherwise, adcl must be read first, then adch , to ensure that the content of the data registers belongs to the same conversion. once adcl is read, adc access to data registers is blocked. this means that if adcl has been read, and a conversion completes before adch is read, neither register is updated and the result fr om the conversion is lost. when adch is read, adc access to the adch and adcl registers is re-ena bled. the adc has its own interrupt which can be triggere d when a conversion completes. when adc access to the data registers is prohibited between reading of adch and adcl, the interrupt will trigger even if the result is lost. 19.3 starting a conversion a single conversion is started by writing a logical one to the adc start conversion bit, adsc. this bit stays high as long as the conversion is in progress and will be cleared by hardware when the conversion is completed. if a different da ta channel is selected while a conversion is in progress, the adc will finish the current conversio n before performing the channel change. alternatively, a conversion can be triggered automa tically by various sources. auto triggering is enabled by setting the adc auto trigger enable bit, adate in adcsra. the trigger source is selected by setting the adc trigger select bits, ad ts in adcsrb (see description of the adts bits for a list of the trigger sources). when a pos itive edge occurs on the selected trigger signal, the adc prescaler is reset and a conversion is star ted. this provides a method of starting con- versions at fixed intervals. if the trigger signal still is set when the conversion completes, a new conversion will not be started. if another positive edge occurs on the trigger signal during con- version, the edge will be ignored. note that an int errupt flag will be set even if the specific interrupt is disabled or the global interrupt enabl e bit in sreg is cleared. a conversion can thus be triggered without causing an interrupt. however, the interrupt flag must be cleared in order to trigger a new conversion at the next interrupt even t.
120 7598d?avr?02/07 attiny25/45/85 auto figure 19-2. adc auto trigger logic using the adc interrupt flag as a trigger source ma kes the adc start a new conversion as soon as the ongoing conversion has finished. the adc the n operates in free running mode, con- stantly sampling and updating the adc data register . the first conversion must be started by writing a logical one to the adsc bit in adcsra. in this mode the adc will perform successive conversions independently of whether the adc interr upt flag, adif is cleared or not. if auto triggering is enabled, single conversions c an be started by writing adsc in adcsra to one. adsc can also be used to determine if a conver sion is in progress. the adsc bit will be read as one during a conversion, independently of h ow the conversion was started. 19.4 prescaling and conversion timing figure 19-3. adc prescaler by default, the successive approximation circuitry requires an input clock frequency between 50 khz and 200 khz to get maximum resolution. if a low er resolution than 10 bits is needed, the input clock frequency to the adc can be higher than 200 khz to get a higher sample rate. the adc module contains a prescaler, which generate s an acceptable adc clock frequency from any cpu frequency above 100 khz. the prescalin g is set by the adps bits in adcsra. adsc adif source 1 source n adts[2:0] conversion logic prescaler start clk adc .. . . edge detector adate 7-bit adc prescaler adc clock source ck adps0adps1 adps2 ck/128 ck/2 ck/4 ck/8 ck/16 ck/32 ck/64 reset aden start
121 7598d?avr?02/07 attiny25/45/85 auto the prescaler starts counting from the moment the a dc is switched on by setting the aden bit in adcsra. the prescaler keeps running for as long as the aden bit is set, and is continuously reset when aden is low. when initiating a single ended conversion by settin g the adsc bit in adcsra, the conversion starts at the following rising edge of the adc cloc k cycle. a normal conversion takes 13 adc clock cycles. the first conversion after the adc is switched on (aden in adcsra is set) takes 25 adc clock cycle s in order to initialize the analog circuitry. the actual sample-and-hold takes place 1.5 adc cloc k cycles after the start of a normal conver- sion and 14.5 adc clock cycles after the start of a n first conversion. when a conversion is complete, the result is written to the adc data reg isters, and adif is set. in single conversion mode, adsc is cleared simultaneously. the software may then set adsc again, and a new conversion will be initiated on the first rising ad c clock edge. when auto triggering is used, the prescaler is rese t when the trigger event occurs. this assures a fixed delay from the trigger event to the start o f conversion. in this mode, the sample-and-hold takes place two adc clock cycles after the rising e dge on the trigger source signal. three addi- tional cpu clock cycles are used for synchronizatio n logic. in free running mode, a new conversion will be star ted immediately after the conversion com- pletes, while adsc remains high. for a summary of c onversion times, see table 19-1 . figure 19-4. adc timing diagram, first conversion (single conver sion mode) figure 19-5. adc timing diagram, single conversion sign and msb of result lsb of result adc clock adsc sample & hold adifadch adcl cycle number aden 1 2 12 13 14 15 16 17 18 19 20 21 22 23 24 25 1 2 first conversion next conversion 3 mux and refs update mux and refs update conversion complete 1 2 3 4 5 6 7 8 9 10 11 12 13 sign and msb of result lsb of result adc clock adsc adif adch adcl cycle number 1 2 one conversion next conversion 3 sample & hold mux and refs update conversion complete mux and refs update
122 7598d?avr?02/07 attiny25/45/85 auto figure 19-6. adc timing diagram, auto triggered conversion figure 19-7. adc timing diagram, free running conversion 19.5 changing channel or reference selection the mux3..0 and refs2..0 bits in the admux register are single buffered through a tempo- rary register to which the cpu has random access. t his ensures that the channels and voltage reference selection only takes place at a safe poin t during the conversion. the channel and volt- age reference selection is continuously updated unt il a conversion is started. once the conversion starts, the channel and voltage referenc e selection is locked to ensure a sufficient sampling time for the adc. continuous updating resu mes in the last adc clock cycle before the table 19-1. adc conversion time condition sample & hold (cycles from start of conversion) conversion time (cycles) first conversion 13.5 25 normal conversions 1.5 13 auto triggered conversions 2 13.5 1 2 3 4 5 6 7 8 9 10 11 12 13 sign and msb of result lsb of result adc clock trigger source adif adch adcl cycle number 1 2 one conversion next conversion conversion complete prescaler reset adate prescaler reset sample & hold mux and refs update 11 12 13 sign and msb of resultlsb of result adc clock adsc adif adch adcl cycle number 1 2 one conversion next conversion 3 4 conversion complete sample & hold mux and refs update
123 7598d?avr?02/07 attiny25/45/85 auto conversion completes (adif in adcsra is set). note that the conversion starts on the following rising adc clock edge after adsc is written. the us er is thus advised not to write new channel or voltage reference selection values to admux unti l one adc clock cycle after adsc is written. if auto triggering is used, the exact time of the t riggering event can be indeterministic. special care must be taken when updating the admux register , in order to control which conversion will be affected by the new settings. if both adate and aden is written to one, an interr upt event can occur at any time. if the admux register is changed in this period, the user cannot tell if the next conversion is based on the old or the new settings. admux can be safely updated in the following ways: a. when adate or aden is cleared. b. during conversion, minimum one adc clock cycle af ter the trigger event. c. after a conversion, before the interrupt flag use d as trigger source is cleared. when updating admux in one of these conditions, the new settings will affect the next adc conversion. 19.5.1 adc input channels when changing channel selections, the user should o bserve the following guidelines to ensure that the correct channel is selected: in single conversion mode, always select the channe l before starting the conversion. the chan- nel selection may be changed one adc clock cycle af ter writing one to adsc. however, the simplest method is to wait for the conversion to co mplete before changing the channel selection. in free running mode, always select the channel bef ore starting the first conversion. the chan- nel selection may be changed one adc clock cycle af ter writing one to adsc. however, the simplest method is to wait for the first conversion to complete, and then change the channel selection. since the next conversion has already st arted automatically, the next result will reflect the previous channel selection. subsequent conversi ons will reflect the new channel selection. 19.5.2 adc voltage reference the voltage reference for the adc (v ref ) indicates the conversion range for the adc. singl e ended channels that exceed v ref will result in codes close to 0x3ff. v ref can be selected as either v cc , or internal 1.1v / 2.56v voltage reference, or ex ternal aref pin. the first adc con- version result after switching voltage reference so urce may be inaccurate, and the user is advised to discard this result. 19.6 adc noise canceler the adc features a noise canceler that enables conv ersion during sleep mode to reduce noise induced from the cpu core and other i/o peripherals . the noise canceler can be used with adc noise reduction and idle mode. to make use of this feature, the following procedure should be used: a. make sure that the adc is enabled and is not busy converting. single conversion mode must be selected and the adc conversion comple te interrupt must be enabled. b. enter adc noise reduction mode (or idle mode). th e adc will start a conversion once the cpu has been halted. c. if no other interrupts occur before the adc conve rsion completes, the adc inter- rupt will wake up the cpu and execute the adc conve rsion complete interrupt
124 7598d?avr?02/07 attiny25/45/85 auto routine. if another interrupt wakes up the cpu befo re the adc conversion is com- plete, that interrupt will be executed, and an adc conversion complete interrupt request will be generated when the adc conversion c ompletes. the cpu will remain in active mode until a new sleep command is executed. note that the adc will not be automatically turned off when entering other sleep modes than idle mode and adc noise reduction mode. the user is advi sed to write zero to aden before enter- ing such sleep modes to avoid excessive power consu mption. 19.6.1 analog input circuitry the analog input circuitry for single ended channel s is illustrated in figure 19-8. an analog source applied to adcn is subjected to the pin capa citance and input leakage of that pin, regard- less of whether that channel is selected as input f or the adc. when the channel is selected, the source must drive the s/h capacitor through the ser ies resistance (combined resistance in the input path). the adc is optimized for analog signals with an out put impedance of approximately 10 k or less. if such a source is used, the sampling time w ill be negligible. if a source with higher imped- ance is used, the sampling time will depend on how long time the source needs to charge the s/h capacitor, with can vary widely. the user is re commended to only use low impedant sources with slowly varying signals, since this minimizes t he required charge transfer to the s/h capacitor. signal components higher than the nyquist frequency (f adc /2) should not be present to avoid distortion from unpredictable signal convolution. t he user is advised to remove high frequency components with a low-pass filter before applying t he signals as inputs to the adc. figure 19-8. analog input circuitry 19.6.2 analog noise canceling techniques digital circuitry inside and outside the device gen erates emi which might affect the accuracy of analog measurements. if conversion accuracy is crit ical, the noise level can be reduced by applying the following techniques: adcn i ih 1..100 k c s/h = 14 pf v cc /2 i il
125 7598d?avr?02/07 attiny25/45/85 auto a. keep analog signal paths as short as possible. ma ke sure analog tracks run over the analog ground plane, and keep them well away fr om high-speed switching digi- tal tracks. b. use the adc noise canceler function to reduce ind uced noise from the cpu. c. if any port pins are used as digital outputs, it is essential that these do not switch while a conversion is in progress. 19.6.3 adc accuracy definitions an n-bit single-ended adc converts a voltage linear ly between gnd and v ref in 2 n steps (lsbs). the lowest code is read as 0, and the highe st code is read as 2 n -1. several parameters describe the deviation from the ideal behavior: ? offset: the deviation of the first transition (0x0 00 to 0x001) compared to the ideal transition (at 0.5 lsb). ideal value: 0 lsb. figure 19-9. offset error ? gain error: after adjusting for offset, the gain e rror is found as the deviation of the last transition (0x3fe to 0x3ff) compared to the ideal t ransition (at 1.5 lsb below maximum). ideal value: 0 lsb figure 19-10. gain error output code v ref input voltage ideal adcactual adc offset error output code v ref input voltage ideal adcactual adc gain error
126 7598d?avr?02/07 attiny25/45/85 auto ? integral non-linearity (inl): after adjusting for offset and gain error, the inl is the maximum deviation of an actual transition compared to an id eal transition for any code. ideal value: 0 lsb. figure 19-11. integral non-linearity (inl) ? differential non-linearity (dnl): the maximum devi ation of the actual code width (the interval between two adjacent transitions) from the ideal co de width (1 lsb). ideal value: 0 lsb. figure 19-12. differential non-linearity (dnl) ? quantization error: due to the quantization of the input voltage into a finite number of codes, a range of input voltages (1 lsb wide) will code to the same value. always 0.5 lsb. ? absolute accuracy: the maximum deviation of an act ual (unadjusted) transition compared to an ideal transition for any code. this is the compo und effect of offset, gain error, differential error, non-linearity, and quantization error. ideal value: 0.5 lsb. 19.7 adc conversion result after the conversion is complete (adif is high), th e conversion result can be found in the adc result registers (adcl, adch). the form of the conv ersion result depends on the type of the output code v ref input voltage ideal adcactual adc inl output code 0x3ff 0x000 0 v ref input voltage dnl 1 lsb
127 7598d?avr?02/07 attiny25/45/85 auto conversio as there are three types of conversions: single ended conversion, unipolar differential conversion and bipolar differential conversion. 19.7.1 single ended conversion for single ended conversion, the result is where v in is the voltage on the selected input pin and v ref the selected voltage reference (see table 19-3 on page 129 and table 19-4 on page 129 ). 0x000 represents analog ground, and 0x3ff represents the selected voltage reference min us one lsb. the result is presented in one- sided form, from 0x3ff to 0x000. 19.7.2 unipolar differential conversion if differential channels and an unipolar input mode are used, the result is where v pos is the voltage on the positive input pin, v neg the voltage on the negative input pin, and v ref the selected voltage reference (see table 19-3 on page 129 and table 19-4 on page 129 ). the voltage on the positive pin must always be l arger than the voltage on the negative pin or otherwise the voltage difference is saturated to zero. the result is presented in one-sided form, from 0x000 (0d) to 0x3ff (+1023d). the gain i s either 1x or 20x. 19.7.3 bipolar differential conversion as default the adc converter operates in the unipol ar input mode, but the bipolar input mode can be selected by writting the bin bit in the adcs rb to one. in the bipolar input mode two- sided voltage differences are allowed and thus the voltage on the negative input pin can also be larger than the voltage on the positive input pin. if differential channels and a bipolar input mode are used, the result is where v pos is the voltage on the positive input pin, v neg the voltage on the negative input pin, and v ref the selected voltage reference. the result is prese nted in two?s complement form, from 0x200 (-512d) through 0x000 (+0d) to 0x1ff (+511d). the gain is either 1x or 20x. however, if the signal is not bipolar by nature (9 bits + sign as the 10th bit), this scheme loses one bit of the converter dynamic range. then, if th e user wants to perform the conversion with the maximum dynamic range, the user can perform a q uick polarity check of the result and use the unipolar differential conversion with selectabl e differential input pairs (see the input polarity reversal mode ie. the ipr bit in the adcsrb registe r on page 135). when the polarity check is performed, it is sufficient to read the msb of the result (adc9 in adch). if the bit is one, the result is negative, and if this bit is zero, the re sult is positive. adc v in 1024 ? v ref -------------------------- = adc v pos v neg ? ( ) 1024 ? v ref --------------------------------------------------- ----- gain ? = adc v pos v neg ? ( ) 512 ? v ref --------------------------------------------------- -- gain ? =
128 7598d?avr?02/07 attiny25/45/85 auto 19.7.4 temperature measurement (preliminary descript ion) the temperature measurement is based on an on-chip temperature sensor that is coupled to a single ended adc4 channel. selecting the adc4 chann el by writing the mux3..0 bits in admux register to ?1111? enables the temperature sensor. the internal 1.1v voltage reference must also be selected for the adc voltage reference sour ce in the temperature sensor measurement. when the temperature sensor is enabled, the adc con verter can be used in single conversion mode to measure the voltage over the temperature se nsor. the measured voltage has a linear relationship to t he temperature as described in table 51. the voltage sensitivity is approximately 1 mv / c and the accuracy of the temperature measurement is +/- 10 c after bandgap calibration. the values described in table 51 are typical values . however, due to the process variation the temperature sensor output voltage varies from one c hip to another. to be capable of achieving more accurate results the temperature measurement c an be calibrated in the application soft- ware. the software calibration requires that a cali bration value is measured and stored in a register or eeprom for each chip, as a part of the production test. the sofware calibration can be done utilizing the formula: where v temp is the adc reading of the temperature sensor signa l, k is a fixed coefficient and t os is the temperature sensor offset value determined a nd stored into eeprom as a part of produc- tion test. 19.7.5 adc multiplexer selection register ? admux ? bit 7..6,4 ? refs2..refs0: voltage reference selec tion bits these bits select the voltage reference (v ref ) for the adc, as shown in table 19-3 . if these bits are changed during a conversion, the change will no t go in effect until this conversion is complete (adif in adcsr is set). whenever these bit s are changed, the next conversion will table 19-2. temperature vs. sensor output voltage (typical case ) temperature / c -45 c +25 c +105 c voltage / mv 242 mv 314 mv 403 mv temperature k v temp t os + ? = bit 7 6 5 4 3 2 1 0 refs1 refs0 adlar refs2 mux3 mux2 mux1 mux0 admux read/write r r/w r/w r r r r/w r/w initial value 0 0 0 0 0 0 0 0
129 7598d?avr?02/07 attiny25/45/85 auto take 25 adc clock cycles. if active channels are us ed, using v cc or an external aref higher than (v cc - 1v) as a voltage reference is not recommended, a s this will affect the adc accuracy. ? bit 5 ? adlar: adc left adjust result the adlar bit affects the presentation of the adc c onversion result in the adc data register. write one to adlar to left adjust the result. other wise, the result is right adjusted. changing the adlar bit will affect the adc data register immedia tely, regardless of any ongoing conver- sions. for a comple te description of this bit, see ?the adc data register ? adcl and adch? on page 131 . ? bits 3:0 ? mux3:0: analog channel and gain selecti on bits the value of these bits selects which combination o f analog inputs are connected to the adc. in case of differential input (adc0 - adc1 or adc2 - a dc3), gain selection is also made with these bits. selecting adc2 or adc0 as both inputs to the differential gain stage enables offset mea- surements. selecting the single-ended channel adc4 enables the temperature sensor. refer to table 19-4 for details. if these bits are changed during a co nversion, the change will not go into effect until this conversion is complete (adif in a dcsra is set). table 19-3. voltage reference selections for adc refs2 refs1 refs0 voltage reference (v ref ) selection 0 0 0 v cc used as voltage reference, disconnected from pb0 ( aref). 0 0 1 external voltage reference at pb0 (aref) pin, inter nal voltage reference turned off. 0 1 0 internal 1.1v voltage reference without external by pass capacitor, disconnected from pb0 (aref). 0 1 1 internal 1.1v voltage reference with external bypas s capacitor at pb0 (aref) pin. 1 1 0 internal 2.56v voltage reference without external b ypass capacitor, disconnected from pb0 (aref). 1 1 1 internal 2.56v voltage reference with external bypa ss capacitor at pb0 (aref) pin. table 19-4. input channel selections mux3..0 single ended input positive differential input negative differential input gain 0000 adc0 (pb5) n/a 0001 adc1 (pb2) 0010 adc2 (pb4) 0011 adc3 (pb3)
130 7598d?avr?02/07 attiny25/45/85 auto 19.7.6 adc control and status register a ? adcsra ? bit 7 ? aden: adc enable writing this bit to one enables the adc. by writing it to zero, the adc is turned off. turning the adc off while a conversion is in progress, will ter minate this conversion. ? bit 6 ? adsc: adc start conversion in single conversion mode, write this bit to one to start each conversion. in free running mode, write this bit to one to start the first conversion . the first conversion after adsc has been written after the adc has been enabled, or if adsc is writt en at the same time as the adc is enabled, will take 25 adc clock cycles instead of the normal 13. this first conversion performs initializa- tion of the adc. adsc will read as one as long as a conversion is in progress. when the conversion is complete, it returns to zero. writing zero to this bit has no effect. ? bit 5 ? adate: adc auto trigger enable when this bit is written to one, auto triggering of the adc is enabled. the adc will start a con- version on a positive edge of the selected trigger signal. the trigger source is selected by setting the adc trigger select bits, adts in adcsrb. ? bit 4 ? adif: adc interrupt flag this bit is set when an adc conversion completes an d the data registers are updated. the adc conversion complete interrupt is executed if the ad ie bit and the i-bit in sreg are set. adif is 0100 n/a adc2 (pb3) adc2 (pb3) 1x 0101 (1) adc2 (pb3) adc2 (pb3) 20x 0110 adc2 (pb3) adc3 (pb4) 1x 0111 adc2 (pb3) adc3 (pb4) 20x 1000 adc0 (pb5) adc0 (pb5) 1x 1001 adc0 (pb5) adc0 (pb5) 20x 1010 adc0 (pb5) adc1 (pb2) 1x 1011 adc0 (pb5) adc1 (pb2) 20x 1100 1.1v/2.56v n/a 1101 0v 1110 n/a 1111 adc4 (2) 1. for offset calibration only . see ?operation? on page 118. 2. for temperature sensor table 19-4. input channel selections mux3..0 single ended input positive differential input negative differential input gain bit 7 6 5 4 3 2 1 0 aden adsc adate adif adie adps2 adps1 adps0 adcsra read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
131 7598d?avr?02/07 attiny25/45/85 auto cleared by hardware when executing the correspondin g interrupt handling vector. alternatively, adif is cleared by writing a logical one to the fla g. beware that if doing a read-modify-write on adcsra, a pending interrupt can be disabled. this a lso applies if the sbi and cbi instructions are used. ? bit 3 ? adie: adc interrupt enable when this bit is written to one and the i-bit in sr eg is set, the adc conversion complete inter- rupt is activated. ? bits 2:0 ? adps2:0: adc prescaler select bits these bits determine the division factor between th e system clock frequency and the input clock to the adc. 19.7.7 the adc data register ? adcl and adch 19.7.7.1 adlar = 0 19.7.7.2 adlar = 1 when an adc conversion is complete, the result is f ound in these two registers. table 19-5. adc prescaler selections adps2 adps1 adps0 division factor 0 0 0 2 0 0 1 2 0 1 0 4 0 1 1 8 1 0 0 16 1 0 1 32 1 1 0 64 1 1 1 128 bit 15 14 13 12 11 10 9 8 ? ? ? ? ? ? adc9 adc8 adch adc7 adc6 adc5 adc4 adc3 adc2 adc1 adc0 adcl 7 6 5 4 3 2 1 0 read/write r r r r r r r r r r r r r r r r initial value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 bit 15 14 13 12 11 10 9 8 adc9 adc8 adc7 adc6 adc5 adc4 adc3 adc2 adch adc1 adc0 ? ? ? ? ? ? adcl 7 6 5 4 3 2 1 0 read/write r r r r r r r r r r r r r r r r initial value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
132 7598d?avr?02/07 attiny25/45/85 auto when adcl is read, the adc data register is not upd ated until adch is read. consequently, if the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read adch. otherwise, adcl must be read first, then adch . the adlar bit in admux, and the muxn bits in admux affect the way the result is read from the registers. if adlar is set, the result is left adjusted. if adlar is cleared (default), the result is right adjusted. ? adc9:0: adc conversion result these bits represent the result from the conversion , as detailed in ?adc conversion result? on page 126 . 19.7.8 adc control and status register b ? adcsrb ? bits 7? bin: bipolar input mode the gain stage is working in the unipolar mode as d efault, but the bipolar mode can be selected by writing the bin bit in the adcsrb register. in t he unipolar mode only one-sided conversions are supported and the voltage on the positive input must always be larger than the voltage on the negative input. otherwise the result is saturat ed to the voltage reference. in the bipolar mode two-sided conversions are supported and the result is represented in the two?s complement form. in the unipolar mode the resolution is 10 bit s and the bipolar mode the resolution is 9 bits + 1 sign bit. ? bits 5 ? ipr: input polarity mode the input polarity mode allows software selectable differential input pairs and full 10 bit adc resolution, in the unipolar input mode, assuming a pre-determined input polarity. if the input polarity is not known it is actually possible to de termine the polarity first by using the bipolar inp ut mode (with 9 bit resolution + 1 sign bit adc measur ement). and once determined, set or clear the polarity reversal bit, as needed, for a succeed ing 10 bit unipolar measurement. ? bits 4..3 ? res: reserved bits these bits are reserved bits in the attiny25/45/85 and will always read as zero. ? bits 2:0 ? adts2:0: adc auto trigger source if adate in adcsra is written to one, the value of these bits selects which source will trigger an adc conversion. if adate is cleared, the adts2:0 settings will have no effect. a conversion will be triggered by the rising edge of the selecte d interrupt flag. note that switching from a trig- ger source that is cleared to a trigger source that is set, will generate a positive edge on the bit 7 6 5 4 3 2 1 0 bin acme ipr ? ? adts2 adts1 adts0 adcsrb read/write r/w r/w r/w r r r/w r/w r/w initial value 0 0 0 0 0 0 0 0
133 7598d?avr?02/07 attiny25/45/85 auto trigger signal. if aden in adcsra is set, this will start a conversion. switching to free running mode (adts[2:0]=0) will not cause a trigger event, even if the adc interrupt flag is set . 19.7.9 digital input disable register 0 ? didr0 ? bits 5..2 ? adc3d..adc0d: adc3..0 digital input di sable when this bit is written logic one, the digital inp ut buffer on the corresponding adc pin is dis- abled. the corresponding pin register bit will alwa ys read as zero when this bit is set. when an analog signal is applied to the adc3..0 pin and the digital input from this pin is not needed, this bit should be written logic one to reduce power con sumption in the digital input buffer. 20. debugwire on-chip debug system 20.1 features ? complete program flow control ? emulates all on-chip functions, both digital and an alog , except reset pin ? real-time operation ? symbolic debugging support (both at c and assembler source level, or for other hlls) ? unlimited number of program break points (using sof tware break points) ? non-intrusive operation ? electrical characteristics identical to real device ? automatic configuration system ? high-speed operation ? programming of non-volatile memories 20.2 overview the debugwire on-chip debug system uses a one-wire, bi-directional interface to control the program flow, execute avr instructions in the cpu a nd to program the different non-volatile memories. table 19-6. adc auto trigger source selections adts2 adts1 adts0 trigger source 0 0 0 free running mode 0 0 1 analog comparator 0 1 0 external interrupt request 0 0 1 1 timer/counter compare match a 1 0 0 timer/counter overflow 1 0 1 timer/counter compare match b 1 1 0 pin change interrupt request bit 7 6 5 4 3 2 1 0 ? ? adc0d adc2d adc3d adc1d ain1d ain0d didr0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
134 7598d?avr?02/07 attiny25/45/85 auto 20.3 physical interface when the debugwire enable (dwen) fuse is programmed and lock bits are unprogrammed, the debugwire system within the target device is ac tivated. the reset port pin is configured as a wire-and (open-drain) bi-directional i/o pin w ith pull-up enabled and becomes the commu- nication gateway between target and emulator. figure 20-1. the debugwire setup figure 20-1 shows the schematic of a target mcu, with debugwir e enabled, and the emulator connector. the system clock is not affected by debu gwire and will always be the clock source selected by the cksel fuses. when designing a system where debugwire will be use d, the following observations must be made for correct operation: ? pull-up resistor on the dw/(reset) line must be in the range of 10k to 20 k . however, the pull-up resistor is optional. ? connecting the reset pin directly to v cc will not work. ? capacitors inserted on the reset pin must be disco nnected when using debugwire. ? all external reset sources must be disconnected. 20.4 software break points debugwire supports program memory break points by t he avr break instruction. setting a break point in avr studio ? will insert a break instruction in the program mem ory. the instruc- tion replaced by the break instruction will be stor ed. when program execution is continued, the stored instruction will be executed before continui ng from the program memory. a break can be inserted manually by putting the break instruction in the program. the flash must be re-programmed each time a break p oint is changed. this is automatically handled by avr studio through the debugwire interfa ce. the use of break points will therefore reduce the falsh data retention. devices used for d ebugging purposes should not be shipped to end customers. dw gnd dw(reset) vcc 1.8 - 5.5v
135 7598d?avr?02/07 attiny25/45/85 auto 20.5 limitations of debugwire the debugwire communication pin (dw) is physically located on the same pin as external reset (reset). an external reset source is therefor e not supported when the debugwire is enabled. the debugwire system accurately emulates all i/o fu nctions when running at full speed, i.e., when the program in the cpu is running. when the cp u is stopped, care must be taken while accessing some of the i/o registers via the debugge r (avr studio). a programmed dwen fuse enables some parts of the cl ock system to be running in all sleep modes. this will increase the power consumption whi le in sleep. thus, the dwen fuse should be disabled when debugwire is not used. 20.6 debugwire related register in i/o memory the following section describes the registers used with the debugwire. 20.6.1 debugwire data register ? dwdr the dwdr register provides a communication channel from the running program in the mcu to the debugger. this register is only accessible b y the debugwire and can therefore not be used as a general purpose register in the normal op erations. 21. self-programming the flash the device provides a self-programming mechanism fo r downloading and uploading program code by the mcu itself. the self-programming can us e any available data interface and associ- ated protocol to read code and write (program) that code into the program memory. the program memory is updated in a page by page fas hion. before programming a page with the data stored in the temporary page buffer, the p age must be erased. the temporary page buffer is filled one word at a time using spm and t he buffer can be filled either before the page erase command or between a page erase and a page wr ite operation: alternative 1, fill the buffer before a page erase ? fill temporary page buffer ? perform a page erase ? perform a page write alternative 2, fill the buffer after page erase ? perform a page erase ? fill temporary page buffer ? perform a page write if only a part of the page needs to be changed, the rest of the page must be stored (for example in the temporary page buffer) before the erase, and then be re-written. when using alternative 1, the boot loader provides an effective read-modify-w rite feature which allows the user software to first read the page, do the necessary changes, a nd then write back the modified data. if alter- native 2 is used, it is not possible to read the ol d data while loading since the page is already bit 7 6 5 4 3 2 1 0 dwdr[7:0] dwdr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
136 7598d?avr?02/07 attiny25/45/85 auto erased. the temporary page buffer can be accessed i n a random sequence. it is essential that the page address used in both the page erase and pa ge write operation is addressing the same page. 21.0.1 performing page erase by spm to execute page erase, set up the address in the z- pointer, write ?00000011? to spmcsr and execute spm within four clock cycles after writing spmcsr. the data in r1 and r0 is ignored. the page address must be written to pcpage in the z -register. other bits in the z-pointer will be ignored during this operation. ? the cpu is halted during the page erase operation. 21.0.2 filling the temporary buffer (page loading) to write an instruction word, set up the address in the z-pointer and data in r1:r0, write ?00000001? to spmcsr and execute spm within four cl ock cycles after writing spmcsr. the content of pcword in the z-register is used to addr ess the data in the temporary buffer. the temporary buffer will auto-erase after a page write operation or by writing the ctpb bit in spmcsr. it is also erased after a system reset. not e that it is not possible to write more than one time to each address without erasing the tempor ary buffer. if the eeprom is written in the middle of an spm pa ge load operation, all data loaded will be lost. 21.0.3 performing a page write to execute page write, set up the address in the z- pointer, write ?00000101? to spmcsr and execute spm within four clock cycles after writing spmcsr. the data in r1 and r0 is ignored. the page address must be written to pcpage. other b its in the z-pointer must be written to zero during this operation. ? the cpu is halted during the page write operation. 21.1 addressing the flash during self-programming the z-pointer is used to address the spm commands. since the flash is organized in pages (see table 22-6 on page 142 ), the program counter can be treated as having two different sections. one se ction, consisting of the least significant bits, is addressing the words within a page, while the most significant bits are addressing the pages. this is shown in figure 21-1 . note that the page erase and page write operation s are addressed independently. therefore it is of major i mportance that the software addresses the same page in both the page erase and page write ope ration. the lpm instruction uses the z-pointer to store the address. since this instruction addresses the flash byte-by-byte, also the lsb (bit z0) of the z- pointer is used. bit 15 14 13 12 11 10 9 8 zh (r31) z15 z14 z13 z12 z11 z10 z9 z8 zl (r30) z7 z6 z5 z4 z3 z2 z1 z0 7 6 5 4 3 2 1 0
137 7598d?avr?02/07 attiny25/45/85 auto figure 21-1. addressing the flash during spm (1) note: 1. the different variables used in figure 21-1 are listed in table 22-6 on page 142 . 21.1.1 store program memory control and status regis ter ? spmcsr the store program memory control and status registe r contains the control bits needed to con- trol the program memory operations. ? bits 7..5 ? res: reserved bits these bits are reserved bits in the attiny25/45/85 and always read as zero. ? bit 4 ? ctpb: clear temporary page buffer if the ctpb bit is written while filling the tempor ary page buffer, the temporary page buffer will be cleared and the data will be lost. ? bit 3 ? rflb: read fuse and lock bits an lpm instruction within three cycles after rflb a nd spmen are set in the spmcsr register, will read either the lock bits or the fuse bits (de pending on z0 in the z-pointer) into the destina- tion register. see ?eeprom write prevents writing to spmcsr? on page 1 38 for details. ? bit 2 ? pgwrt: page write if this bit is written to one at the same time as s pmen, the next spm instruction within four clock cycles executes page write, with the data stored in the temporary buffer. the page address is taken from the high part of the z-pointer. the data in r1 and r0 are ignored. the pgwrt bit program memory 0 1 15 z - register bit 0 zpagemsb word addresswithin a page page addresswithin the flash zpcmsb instruction wor d pag e pcword[p age msb:0]: 0001 02 pag eend pag e pcword pcp age pcmsb pagemsb programcounter bit 7 6 5 4 3 2 1 0 ? ? ? ctpb rflb pgwrt pgers spmen spmcsr read/write r r r r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
138 7598d?avr?02/07 attiny25/45/85 auto will auto-clear upon completion of a page write, or if no spm instruction is executed within four clock cycles. the cpu is halted during the entire p age write operation. ? bit 1 ? pgers: page erase if this bit is written to one at the same time as s pmen, the next spm instruction within four clock cycles executes page erase. the page address is tak en from the high part of the z-pointer. the data in r1 and r0 are ignored. the pgers bit will a uto-clear upon completion of a page erase, or if no spm instruction is executed within four cl ock cycles. the cpu is halted during the entire page write operation. ? bit 0 ? spmen: store program memory enable this bit enables the spm instruction for the next f our clock cycles. if written to one together with either ctpb, rflb, pgwrt, or pgers, the following s pm instruction will have a special meaning, see description above. if only spmen is wr itten, the following spm instruction will store the value in r1:r0 in the temporary page buff er addressed by the z-pointer. the lsb of the z-pointer is ignored. the spmen bit will auto-c lear upon completion of an spm instruction, or if no spm instruction is executed within four cl ock cycles. during page erase and page write, the spmen bit remains high until the operation is c ompleted. writing any other combination than ?10001?, ?01001? , ?00101?, ?00011? or ?00001? in the lower five bits will have no effect. 21.1.2 eeprom write prevents writing to spmcsr note that an eeprom write operation will block all software programming to flash. reading the fuses and lock bits from software will also be prev ented during the eeprom write operation. it is recommended that the user checks the status bit (eewe) in the eecr register and verifies that the bit is cleared before writing to the spmcs r register. 21.1.3 reading the fuse and lock bits from software it is possible to read both the fuse and lock bits from software. to read the lock bits, load the z-pointer with 0x0001 and set the rflb and spmen bi ts in spmcsr. when an lpm instruction is executed within three cpu cycles after the rflb and spmen bits are set in spmcsr, the value of the lock bits will be loaded in the destin ation register. the rflb and spmen bits will auto-clear upon completion of reading the lock bits or if no lpm instruction is executed within three cpu cycles or no spm instruction is executed within four cpu cycles. when rflb and spmen are cleared, lpm will work as described in th e instruction set manual. the algorithm for reading the fuse low byte is simi lar to the one described above for reading the lock bits. to read the fuse low byte, load the z-pointer with 0x0000 and set the rflb and spmen bits in spmcsr. when an lpm instruction is ex ecuted within three cycles after the rflb and spmen bits are set in the spmcsr, the valu e of the fuse low byte (flb) will be loaded in the destination register as shown below. refer to table 22-5 on page 141 for a detailed description and mapping of the fuse low by te. similarly, when reading the fuse high byte, load 0x 0003 in the z-pointer. when an lpm instruc- tion is executed within three cycles after the rflb and spmen bits are set in the spmcsr, the bit 7 6 5 4 3 2 1 0 rd ? ? ? ? ? ? lb2 lb1 bit 7 6 5 4 3 2 1 0 rd flb7 flb6 flb5 flb4 flb3 flb2 flb1 flb0
139 7598d?avr?02/07 attiny25/45/85 auto value of the fuse high byte (fhb) will be loaded in the destination register as shown below. refer to table xxx on page xxx for detailed description and mapping of the fuse h igh byte. fuse and lock bits that are programmed, will be rea d as zero. fuse and lock bits that are unprogrammed, will be read as one. 21.1.4 preventing flash corruption during periods of low v cc , the flash program can be corrupted because the su pply voltage is too low for the cpu and the flash to operate proper ly. these issues are the same as for board level systems using the flash, and the same design solutions should be applied. a flash program corruption can be caused by two sit uations when the voltage is too low. first, a regular write sequence to the flash requires a mini mum voltage to operate correctly. secondly, the cpu itself can execute instructions incorrectly , if the supply voltage for executing instructions is too low. flash corruption can easily be avoided by following these design recommendations (one is sufficient): 1. keep the avr reset active (low) during periods of insufficient power supply voltage. this can be done by enabling the internal brown-out detector (bod) if the operating voltage matches the detection level. if not, an ext ernal low v cc reset protection circuit can be used. if a reset occurs while a write operat ion is in progress, the write operation will be completed provided that the power supply vo ltage is sufficient. 2. keep the avr core in power-down sleep mode during periods of low v cc . this will pre- vent the cpu from attempting to decode and execute instructions, effectively protecting the spmcsr register and thus the flash from uninten tional writes. 21.1.5 programming time for flash when using spm the calibrated rc oscillator is used to time flash accesses. table 21-1 shows the typical pro- gramming time for flash accesses from the cpu. 22. memory programming this section describes the different methods for pr ogramming the attiny25/45/85 memories. 22.1 program and data memory lock bits the attiny25/45/85 provides two lock bits which can be left unprogrammed (?1?) or can be pro- grammed (?0?) to obtain the additional security lis ted in table 22-2 . the lock bits can only be erased to ?1? with the chip erase command. bit 7 6 5 4 3 2 1 0 rd fhb7 fhb6 fhb5 fhb4 fhb3 fhb2 fhb1 fhb0 table 21-1. spm programming time symbol min programming time max programming time flash write (page erase, page write, and write lock bits by spm) 3.7 ms 4.5 ms
140 7598d?avr?02/07 attiny25/45/85 auto program memory can be read out via the debugwire in terface when the dwen fuse is pro- grammed, even if the lock bits are set. thus, when lock bit security is required, should always debugwire be disabled by clearing the dwen fuse. note: 1. ?1? means unprogrammed, ?0? means programmed notes: 1. program the fuse bits before programming th e lb1 and lb2. 2. ?1? means unprogrammed, ?0? means programmed 22.2 fuse bytes the attiny25/45/85 has three fuse bytes. table 22-4 , table 22-5 and table61 describe briefly the functionality of all the fuses and how they are mapped into the fuse bytes. note that the fuses are read as logical zero, ?0?, if they are pr ogrammed. table 22-1. lock bit byte (1) lock bit byte bit no description default value 7 ? 1 (unprogrammed) 6 ? 1 (unprogrammed) 5 ? 1 (unprogrammed) 4 ? 1 (unprogrammed) 3 ? 1 (unprogrammed) 2 ? 1 (unprogrammed) lb2 1 lock bit 1 (unprogrammed) lb1 0 lock bit 1 (unprogrammed) table 22-2. lock bit protection modes (1)(2) memory lock bits protection type lb mode lb2 lb1 1 1 1 no memory lock features enabled. 2 1 0 further programming of the flash and eeprom is disa bled in high-voltage and serial programming mode. the fuse bits are locked in both serial and high-voltage programming mode. (1) debugwire is disabled. 3 0 0 further programming and verification of the flash a nd eeprom is disabled in high-voltage and serial progr amming mode. the fuse bits are locked in both serial and h igh-voltage programming mode. (1) debugwire is disabled. table 22-3. fuse extended byte fuse high byte bit no description default value 7 - 1 (unprogrammed) 6 - 1 (unprogrammed) 5 - 1 (unprogrammed) 4 - 1 (unprogrammed) 3 - 1 (unprogrammed)
141 7598d?avr?02/07 attiny25/45/85 auto notes: 1. see ?alternate functions of port b? on page 54 for description of rstdisbl and dwen fuses. 2. dwen must be unprogrammed when lock bit security is required. see ?program and data memory lock bits? on page 139. 3. the spien fuse is not accessible in spi programmi ng mode. 4. see ?watchdog timer control register ? wdtcr? on page 4 1 for details. 5. see table 8-2 on page 38 for bodlevel fuse decoding. 6. when programming the rstdisbl fuse, high-voltage serial programming has to be used to change fuses to perform further programming. notes: 1. see ?system clock prescaler? on page 29 for details. 2. the ckout fuse allows the system clock to be outp ut on portb4. see ?clock output buffer? on page 30 for details. 3. the default value of sut1..0 results in maximum s tart-up time for the default clock source. see table 6-7 on page 26 for details. 2 - 1 (unprogrammed) 1 - 1 (unprogrammed) selfprgen 0 self-programming enable 1 (unprogrammed) table 22-4. fuse high byte fuse high byte bit no description default value rstdisbl (1) 7 external reset disable 1 (unprogrammed) dwen (2) 6 debugwire enable 1 (unprogrammed) spien (3) 6 enable serial program and data downloading 0 (programmed, spi prog. enabled) wdton (4) 4 watchdog timer always on 1 (unprogrammed) eesave 3 eeprom memory is preserved through the chip erase 1 (unprogrammed, eeprom not preserved) bodlevel2 (5) 2 brown-out detector trigger level 1 (unprogrammed) bodlevel1 (5) 1 brown-out detector trigger level 1 (unprogrammed) bodlevel0 (5) 0 brown-out detector trigger level 1 (unprogrammed) table 22-5. fuse low byte fuse low byte bit no description default value ckdiv8 (1) 7 divide clock by 8 0 (unprogrammed) ckout (2) 6 clock output enable 1 (unprogrammed) sut1 5 select start-up time 1 (unprogrammed) (3) sut0 4 select start-up time 0 (programmed) (3) cksel3 3 select clock source 0 (programmed) (4) cksel2 2 select clock source 0 (programmed) (4) cksel1 1 select clock source 1 (unprogrammed) (4) cksel0 0 select clock source 0 (programmed) (4) table 22-3. fuse extended byte fuse high byte bit no description default value
142 7598d?avr?02/07 attiny25/45/85 auto 4. the default setting of cksel1..0 results in inter nal rc oscillator @ 8.0 mhz. see table 6-6 on page 26 for details. the status of the fuse bits is not affected by chip erase. note that the fuse bits are locked if lock bit1 (lb1) is programmed. program the fuse bit s before programming the lock bits. 22.2.1 latching of fuses the fuse values are latched when the device enters programming mode and changes of the fuse values will have no effect until the part leav es programming mode. this does not apply to the eesave fuse which will take effect once it is p rogrammed. the fuses are also latched on power-up in normal mode. 22.3 signature bytes all atmel microcontrollers have a three-byte signat ure code which identifies the device. this code can be read in both serial and high-voltage pr ogramming mode, also when the device is locked. the three bytes reside in a separate addres s space. 22.3.1 attiny25 signature bytes 1. 0x000: 0x1e (indicates manufactured by atmel). 2. 0x001: 0x91 (indicates 2 kb flash memory). 3. 0x002: 0x08 (indicates attiny25 device when 0x001 is 0x91). 22.3.2 attiny45 signature bytes 1. 0x000: 0x1e (indicates manufactured by atmel). 2. 0x001: 0x92 (indicates 4 kb flash memory). 3. 0x002: 0x06 (indicates attiny45 device when 0x001 is 0x92). 22.3.3 attiny85 signature bytes 1. 0x000: 0x1e (indicates manufactured by atmel). 2. 0x001: 0x93 (indicates 8 kb flash memory). 3. 0x002: 0x0b (indicates attiny85 device when 0x001 is 0x93). 22.4 calibration byte signature area of the attiny25/45/85 has one byte o f calibration data for the internal rc oscilla- tor. this byte resides in the high byte of address 0x000. during reset, this byte is automatically written into the osccal register to ensure correct frequency of the calibrated rc oscillator. 22.5 page size table 22-6. no. of words in a page and no. of pages in the flas h device flash size page size pcword no. of pages pcpage pc msb attiny25 1k words (2k bytes) 16 words pc[3:0] 64 pc[9:4] 9 attiny45 2k words (4k bytes) 32 words pc[4:0] 64 pc[10:5] 10 attiny85 4k words (8k bytes) 32 words pc[4:0] 128 pc[11:5] 11
143 7598d?avr?02/07 attiny25/45/85 auto 22.6 serial downloading both the flash and eeprom memory arrays can be prog rammed using the serial spi bus while reset is pulled to gnd. the serial interface consists of pins sck, mosi (input) and miso (out- put). after reset is set low, the programming enable instruction nee ds to be executed first before program/erase operations can be executed. no te, in table 22-8 on page 143 , the pin mapping for spi programming is listed. not all part s use the spi pins dedicated for the internal spi interface. figure 22-1. serial programming and verify (1) notes: 1. if the device is clocked by the internal os cillator, it is no need to connect a clock source t o the clki pin. when programming the eeprom, an auto-erase cycle is built into the self-timed programming operation (in the serial mode only) and there is no need to first execute the chip erase instruction. the chip erase operation turns the con tent of every memory location in both the program and eeprom arrays into 0xff. depending on cksel fuses, a valid clock must be pre sent. the minimum low and high periods for the serial clock (sck) input are defined as fol lows: table 22-7. no. of words in a page and no. of pages in the eepr om device eeprom size page size pcword no. of pages pcpage eeamsb attiny25 128 bytes 4 bytes eea[1:0] 32 eea[6:2] 6 attiny45 256 bytes 4 bytes eea[1:0] 64 eea[7:2] 7 attiny85 512 bytes 4 bytes eea[1:0] 128 eea[8:2] 8 table 22-8. pin mapping serial programming symbol pins i/o description mosi pb0 i serial data in miso pb1 o serial data out sck pb2 i serial clock vcc gnd sck miso mosi reset +1.8 - 5.5v
144 7598d?avr?02/07 attiny25/45/85 auto low: > 2 cpu clock cycles for f ck < 12 mhz, 3 cpu clock cycles for f ck >= 12 mhz high: > 2 cpu clock cycles for f ck < 12 mhz, 3 cpu clock cycles for f ck >= 12 mhz 22.6.1 serial programming algorithm when writing serial data to the attiny25/45/85, dat a is clocked on the rising edge of sck. when reading data from the attiny25/45/85, data is clocked on the falling edge of sck. see figure 22-2 and figure 22-3 for timing details. to program and verify the attiny25/45/85 in the ser ial programming mode, the following sequence is recommended (see four byte instruction formats in table 22-10 ): 1. power-up sequence: apply power between v cc and gnd while reset and sck are set to ?0?. in some sys- tems, the programmer can not guarantee that sck is held low during power-up. in this case, reset must be given a positive pulse of at least two cpu clock cycles duration after sck has been set to ?0?. 2. wait for at least 20 ms and enable serial program ming by sending the programming enable serial instruction to pin mosi. 3. the serial programming instructions will not work if the communication is out of syn- chronization. when in sync. the second byte (0x53), will echo back when issuing the third byte of the programming enable instruction. w hether the echo is correct or not, all four bytes of the instruction must be transmitted. if the 0x53 did not echo back, give reset a positive pulse and issue a new programming enabl e command. 4. the flash is programmed one page at a time. the m emory page is loaded one byte at a time by supplying the 5 lsb of the address and da ta together with the load program memory page instruction. to ensure correct loading of the page, the data low byte must be loaded before data high byte is applied for a gi ven address. the program memory page is stored by loading the write program memory page instruction with the 6 msb of the address. if polling (rdy/bsy) is not used, the user must wait at least t wd_flash before issuing the next page. (see table 22-9 .) accessing the serial programming interface before the flash write operation complete s can result in incorrect programming. 5. a: the eeprom array is programmed one byte at a time by supplying the address and data together with the appropriate write instructio n. an eeprom memory location is first automatically erased before new data is writt en. if polling (rdy/bsy) is not used, the user must wait at least t wd_eeprom before issuing the next byte. (see table 22-9 .) in a chip erased device, no 0xffs in the data file(s) need to be programmed. b: the eeprom array is programmed one page at a time. the memory page is loaded one byte at a time by supplying the 2 lsb of the ad dress and data together with the load eeprom memory page instruction. the eeprom mem ory page is stored by loading the write eeprom memory page instruction wi th the 6 msb of the address. when using eeprom page access only byte locations l oaded with the load eeprom memory page instruction is altered. the remaining l ocations remain unchanged. if poll- ing (rdy/bsy) is not used, the used must wait at least t wd_eeprom before issuing the next page (see table 22-7 ). in a chip erased device, no 0xff in the data fil e(s) need to be programmed. 6. any memory location can be verified by using the read instruction which returns the content at the selected address at serial output mi so. 7. at the end of the programming session, reset can be set high to commence normal operation.
145 7598d?avr?02/07 attiny25/45/85 auto 8. power-off sequence (if needed): set reset to ?1?. turn v cc power off. figure 22-2. serial programming waveforms table 22-9. minimum wait delay before writing the next flash or eeprom location symbol minimum wait delay t wd_flash 4.5 ms t wd_eeprom 4.0 ms t wd_erase 4.0 ms t wd_fuse 4.5 ms msbmsb lsblsb serial clock input (sck) serial data input (mosi) (miso) sample serial data output
146 7598d?avr?02/07 attiny25/45/85 auto table 22-10. serial programming instruction set instruction instruction format operation byte 1 byte 2 byte 3 byte4 programming enable 1010 1100 0101 0011 xxxx xxxx xxxx xxxx enable serial programming after reset goes low. chip erase 1010 1100 100x xxxx xxxx xxxx xxxx xxxx chip erase eeprom and flash. read program memory 0010 h 000 0000 000 a bbbb bbbb oooo oooo read h (high or low) data o from program memory at word address a:b . load program memory page 0100 h 000 000x xxxx xxx b bbbb iiii iiii write h (high or low) data i to program memory page at word address b . data low byte must be loaded before data high byte is applied within the same address. write program memory page 0100 1100 0000 000 a bb xx xxxx xxxx xxxx write program memory page at address a:b . read eeprom memory 1010 0000 000x xxxx xx bb bbbb oooo oooo read data o from eeprom memory at address b . write eeprom memory 1100 0000 000x xxxx xx bb bbbb iiii iiii write data i to eeprom memory at address b . load eeprom memory page (page access) 1100 0001 0000 0000 0000 00 bb iiii iiii load data i to eeprom memory page buffer. after data is loaded, program eeprom page. write eeprom memory page (page access) 1100 0010 00xx xxxx xx bb bb00 xxxx xxxx write eeprom page at address b . read lock bits 0101 1000 0000 0000 xxxx xxxx xx oo oooo read lock bits. ?0? = programmed, ?1? = unprogrammed. see table 22-1 on page 140 for details. write lock bits 1010 1100 111x xxxx xxxx xxxx 11 ii iiii write lock bits. set bits = ?0? to program lock bits. see table 22-1 on page 140 for details. read signature byte 0011 0000 000x xxxx xxxx xx bb oooo oooo read signature byte o at address b . write fuse bits 1010 1100 1010 0000 xxxx xxxx iiii iiii set bits = ?0? to program, ?1? to unprogram. see table 22- 5 on page 141 for details. write fuse high bits 1010 1100 1010 1000 xxxx xxxx iiii iiii set bits = ?0? to program, ?1? to unprogram. see table 22- 4 on page 141 for details. write extended fuse bits 1010 1100 1010 0100 xxxx xxxx xxxx xxxi set bits = ?0? to program, ?1? to unprogram. see table 22- 3 on page 140 for details.
147 7598d?avr?02/07 attiny25/45/85 auto note: a = address high bits, b = address low bits, h = 0 - low byte, 1 - high byte, o = data out, i = data in, x = don?t care 22.6.2 serial programming characteristics figure 22-3. serial programming timing read fuse bits 0101 0000 0000 0000 xxxx xxxx oooo oooo read fuse bits. ?0? = programmed, ?1? = unprogrammed. see table 22-5 on page 141 for details. read fuse high bits 0101 1000 0000 1000 xxxx xxxx oooo oooo read fuse high bits. ?0? = pro-grammed, ?1? = unprogrammed. see table 22-4 on page 141 for details. read extended fuse bits 0101 0000 0000 1000 xxxx xxxx oooo oooo read extended fuse bits. ?0? = pro-grammed, ?1? = unprogrammed. see table 22-3 on page 140 for details. read calibration byte 0011 1000 000x xxxx 0000 0000 oooo oooo read calibration byte poll rdy/bsy 1111 0000 0000 0000 xxxx xxxx xxxx xxx o if o = ?1?, a programming operation is still busy. wait until this bit returns to ?0? before applying another command. table 22-10. serial programming instruction set instruction instruction format operation byte 1 byte 2 byte 3 byte4 table 22-11. serial programming characteristics, t a = -40 c to 125 c, v cc = 2.7 - 5.5v (unless otherwise noted) symbol parameter min typ max units 1/t clcl oscillator frequency (attiny25/45/85v) 0 4 mhz t clcl oscillator period (attiny25/45/85v) 250 ns 1/t clcl oscillator frequency (attiny25/45/85l, vcc = 2.7 - 5.5v) 0 10 mhz t clcl oscillator period (attiny25/45/85l, vcc = 2.7 - 5.5v) 100 ns mosimiso sck t ovsh t shsl t slsh t shox t sliv
148 7598d?avr?02/07 attiny25/45/85 auto note: 1. 2 t clcl for f ck < 12 mhz, 3 t clcl for f ck >= 12 mhz 22.7 high-voltage serial programming this section describes how to program and verify fl ash program memory, eeprom data mem- ory, lock bits and fuse bits in the attiny25/45/85. figure 22-4. high-voltage serial programming the minimum period for the serial clock input (sci) during high-voltage serial programming is 220 ns. 1/t clcl oscillator frequency (attiny25/45/85, v cc = 4.5v - 5.5v) 0 20 mhz t clcl oscillator period (attiny25/45/85, v cc = 4.5v - 5.5v) 50 ns t shsl sck pulse width high 2 t clcl* ns t slsh sck pulse width low 2 t clcl * ns t ovsh mosi setup to sck high t clcl ns t shox mosi hold after sck high 2 t clcl ns t sliv sck low to miso valid 15 ns table 22-11. serial programming characteristics, t a = -40 c to 125 c, v cc = 2.7 - 5.5v (unless otherwise noted) symbol parameter min typ max units table 22-12. pin name mapping signal name in high-voltage serial programming mode pin name i/o function sdi pb0 i serial data input sii pb1 i serial instruction input sdo pb2 o serial data output sci pb3 i serial clock input (min. 220ns period) cc
149 7598d?avr?02/07 attiny25/45/85 auto 22.8 high-voltage serial programming algorithm to program and verify the attiny25/45/85 in the hig h-voltage serial programming mode, the fol- lowing sequence is recommended (see instruction for mats in table 22-15 ): 22.8.1 enter high-voltage serial programming mode the following algorithm puts the device in high-vol tage serial programming mode: 1. apply 4.5 - 5.5v between v cc and gnd. 2. set reset pin to ?0? and toggle sci at least six times. 3. set the prog_enable pins listed in table 22-13 to ?000? and wait at least 100 ns. 4. apply v hvrst - 5.5v to reset. keep the prog_enable pins unchang ed for at least t hvrst after the high-voltage has been applied to ensure the prog_enable signature has been latched. 5. shortly after latching the prog_enable signature, the device will activly output data on the prog_enable[2]/sdo pin, and the resulting drive contention may increase the power consumption. to minimize this drive contentio n, release the prog_enable[2] pin after t hvrst has elapsed. 6. wait at least 50 s before giving any serial inst ructions on sdi/sii. 22.8.2 considerations for efficient programming the loaded command and address are retained in the device during programming. for efficient programming, the following should be considered. ? the command needs only be loaded once when writing or reading multiple memory locations. ? skip writing the data value 0xff that is the conte nts of the entire eeprom (unless the eesave fuse is programmed) and flash after a chip e rase. ? address high byte needs only be loaded before prog ramming or reading a new 256 word window in flash or 256 byte eeprom. this considerat ion also applies to signature bytes reading. table 22-13. pin values used to enter programming mode pin symbol value sdi prog_enable[0] 0 sii prog_enable[1] 0 sdo prog_enable[2] 0 table 22-14. high-voltage reset characteristics supply voltage reset pin high-voltage threshold minimum high-voltage period for latching prog_enable v cc v hvrst t hvrst 4.5v 11.5v 100 ns 5.5v 11.5v 100 ns
150 7598d?avr?02/07 attiny25/45/85 auto 22.8.3 chip erase the chip erase will erase the flash and eeprom (1) memories plus lock bits. the lock bits are not reset until the program memory has been complet ely erased. the fuse bits are not changed. a chip erase must be performed before the flash and/or eeprom are re- programmed. note: 1. the eeprom memory is preserved during chip e rase if the eesave fuse is programmed. 1. load command ?chip erase? (see table 22-15 ). 2. wait after instr. 3 until sdo goes high for the ? chip erase? cycle to finish. 3. load command ?no operation?. 22.8.4 programming the flash the flash is organized in pages, see table 22-10 on page 146 . when programming the flash, the program data is latched into a page buffer. thi s allows one page of program data to be pro- grammed simultaneously. the following procedure des cribes how to program the entire flash memory: 1. load command ?write flash? (see table 22-15 ). 2. load flash page buffer. 3. load flash high address and program page. wait a fter instr. 3 until sdo goes high for the ?page programming? cycle to finish. 4. repeat 2 through 3 until the entire flash is prog rammed or until all data has been programmed. 5. end page programming by loading command ?no opera tion?. when writing or reading serial data to the attiny25 /45/85, data is clocked on the rising edge of the serial clock, see figure 22-6 , figure 22-7 and table 22-16 for details. figure 22-5. addressing the flash which is organized in pages program memory word addresswithin a page page addresswithin the flash instruction wor d pag e pcword[p age msb:0]: 0001 02 pag eend pag e pcword pcp age pcmsb pagemsb programcounter
151 7598d?avr?02/07 attiny25/45/85 auto figure 22-6. high-voltage serial programming waveforms 22.8.5 programming the eeprom the eeprom is organized in pages, see table 22-11 on page 147 . when programming the eeprom, the data is latched into a page buffer. thi s allows one page of data to be pro- grammed simultaneously. the programming algorithm f or the eeprom data memory is as follows (refer to table 22-15 ): 1. load command ?write eeprom?. 2. load eeprom page buffer. 3. program eeprom page. wait after instr. 2 until sd o goes high for the ?page pro- gramming? cycle to finish. 4. repeat 2 through 3 until the entire eeprom is pro grammed or until all data has been programmed. 5. end page programming by loading command ?no opera tion?. 22.8.6 reading the flash the algorithm for reading the flash memory is as fo llows (refer to table 22-15 ): 1. load command "read flash". 2. read flash low and high bytes. the contents at th e selected address are available at serial output sdo. 22.8.7 reading the eeprom the algorithm for reading the eeprom memory is as f ollows (refer to table 22-15 ): 1. load command ?read eeprom?. 2. read eeprom byte. the contents at the selected ad dress are available at serial out- put sdo. 22.8.8 programming and reading the fuse and lock bit s the algorithms for programming and reading the fuse low/high bits and lock bits are shown in table 22-15 . 22.8.9 reading the signature bytes and calibration b yte the algorithms for reading the signature bytes and calibration byte are shown in table 22-15 . 22.8.10 power-off sequence set sci to ?0?. set reset to ?1?. turn v cc power off. msbmsb msb lsb lsb lsb 0 1 2 3 4 5 6 7 8 9 10 sdi pb0 sii pb1 sdo pb2 sci pb3
152 7598d?avr?02/07 attiny25/45/85 auto table 22-15. high-voltage serial programming instruction set for attiny25/45/85 instruction instruction format operation remarks instr.1/5 instr.2/6 instr.3 instr.4 chip erase sd i sii sd o 0_1000_0000_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx wait after instr.3 until sdo goes high for the chip erase cycle to finish. load ?write flash? command sd i sii sd o 0_0001_0000_00 0_0100_1100_00 x_xxxx_xxxx_xx enter flash programming code. load flash page buffer sd i sii sd o 0_ bbbb _ bbbb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_ eeee _ eeee _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_ dddd _ dddd _00 0_0011_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1101_00 x_xxxx_xxxx_xx repeat after instr. 1 - 5 until the entire page buffer is filled or until all data within the page is filled. see note 1. sd i sii sd o 0_0000_0000_00 0_0111_1100_00 x_xxxx_xxxx_xx instr 5. load flash high address and program page sd i sii sd o 0_0000_000 a _00 0_0001_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx wait after instr 3 until sdo goes high. repeat instr. 2 - 3 for each loaded flash page until the entire flash or all data is programmed. repeat instr. 1 for a new 256 byte page. see note 1. load ?read flash? command sd i sii sd o 0_0000_0010_00 0_0100_1100_00 x_xxxx_xxxx_xx enter flash read mode. read flash low and high bytes sd i sii sd o 0_ bbbb _ bbbb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_0000_000 a _00 0_0001_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 q _ qqqq _ qqq x_xx repeat instr. 1, 3 - 6 for each new address. repeat instr. 2 for a new 256 byte page. sd i sii sd o 0_0000_0000_00 0_0111_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1100_00 p _ pppp _ ppp x_xx instr 5 - 6. load ?write eeprom? command sd i sii sd o 0_0001_0001_00 0_0100_1100_00 x_xxxx_xxxx_xx enter eeprom programming mode.
153 7598d?avr?02/07 attiny25/45/85 auto load eeprom page buffer sd i sii sd o 0_00 bb _ bbbb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_ eeee _ eeee _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1101_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx repeat instr. 1 - 4 until the entire page buffer is filled or until all data within the page is filled. see note 2. program eeprom page sd i sii sd o 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx wait after instr. 2 until sdo goes high. repeat instr. 1 - 2 for each loaded eeprom page until the entire eeprom or all data is programmed. write eeprom byte sd i sii sd o 0_00 bb _ bbbb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_ eeee _ eeee _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1101_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx repeat instr. 1 - 5 for each new address. wait after instr. 5 until sdo goes high. see note 3. sd i sii sd o 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx instr. 5 load ?read eeprom? command sd i sii sd o 0_0000_0011_00 0_0100_1100_00 x_xxxx_xxxx_xx enter eeprom read mode. read eeprom byte sd i sii sd o 0_ bbbb _ bbbb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_ aaaa _ aaaa _00 0_0001_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 q _ qqqq _ qqq 0_00 repeat instr. 1, 3 - 4 for each new address. repeat instr. 2 for a new 256 byte page. write fuse low bits sd i sii sd o 0_0100_0100_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_ a987 _ 6543 _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx wait after instr. 4 until sdo goes high. write a - 3 = ?0? to program the fuse bit. write fuse high bits sd i sii sd o 0_0100_0000_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_000 f _ edcb _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1100_00 x_xxxx_xxxx_xx wait after instr. 4 until sdo goes high. write f - b = ?0? to program the fuse bit. write lock bits sd i sii sd o 0_0010_0000_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_00 21 _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx wait after instr. 4 until sdo goes high. write 2 - 1 = ?0? to program the lock bit. read fuse low bits sd i sii sd o 0_0000_0100_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 a _ 9876 _ 543 x_xx reading a - 3 = ?0? means the fuse bit is programmed. table 22-15. high-voltage serial programming instruction set for attiny25/45/85 (continued) instruction instruction format operation remarks instr.1/5 instr.2/6 instr.3 instr.4
154 7598d?avr?02/07 attiny25/45/85 auto note: a = address high bits, b = address low bits, d = data in high bits, e = data in low bits, p = data out high bits, q = data out low bits, x = don?t care, 1 = lock bit1, 2 = lock bit2, 3 = cksel0 fuse, 4 = cksel1 fuse, 5 = sut0 fuse, 6 = sut1 fuse, 7 = ckdiv8, fuse, 8 = wdton fuse, 9 = eesave fuse, a = spien fuse, b = rstdisbl fuse, c = bodlevel0 fuse, d = bodlevel1 fuse, e = monen fuse, f = spmen fuse notes: 1. for page sizes less than 256 words, parts o f the address (bbbb_bbbb) will be parts of the page address. 2. for page sizes less than 256 bytes, parts of the address (bbbb_bbbb) will be parts of the page addre ss. 3. the eeprom is written page-wise. but only the byt es that are loaded into the page are actually writt en to the eeprom. page-wise eeprom access is more efficient when mult iple bytes are to be written to the same page. note that auto-erase of eeprom is not available in high-voltage serial p rogramming, only in spi programming. read fuse high bits sd i sii sd o 0_0000_0100_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1010_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1110_00 x_xx fe _ dcb x_xx reading f - b = ?0? means the fuse bit is programmed. read lock bits sd i sii sd o 0_0000_0100_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1100_00 x_xxxx_x 21 x_xx reading 2, 1 = ?0? means the lock bit is programmed. read signature bytes sd i sii sd o 0_0000_1000_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_00 bb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 q _ qqqq _ qqq x_xx repeats instr 2 4 for each signature byte address. read calibration byte sd i sii sd o 0_0000_1000_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0000_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1100_00 p _ pppp _ ppp x_xx load ?no operation? command sd i sii sd o 0_0000_0000_00 0_0100_1100_00 x_xxxx_xxxx_xx table 22-15. high-voltage serial programming instruction set for attiny25/45/85 (continued) instruction instruction format operation remarks instr.1/5 instr.2/6 instr.3 instr.4
155 7598d?avr?02/07 attiny25/45/85 auto 22.9 high-voltage serial programming characteristics figure 22-7. high-voltage serial programming timing table 22-16. high-voltage serial programming characteristics t a = 25 c 10%, v cc = 5.0v 10% (unless otherwise noted) symbol parameter min typ max units t shsl sci (pb3) pulse width high 110 ns t slsh sci (pb3) pulse width low 110 ns t ivsh sdi (pb0), sii (pb1) valid to sci (pb3) high 50 ns t shix sdi (pb0), sii (pb1) hold after sci (pb3) high 50 ns t shov sci (pb3) high to sdo (pb2) valid 16 ns t wlwh_pfb wait after instr. 3 for write fuse bits 2.5 ms ck cc
156 7598d?avr?02/07 attiny25/45/85 auto 23. electrical characteristics 23.1 absolute maximum ratings* operating temperature.............................. .... -40 c to +125 c *notice: stresses beyond those listed under ?absolut e maximum ratings? may cause permanent dam- age to the device. this is a stress rating only and functional operation of the device at these or other conditions beyond those indicated in the operational sections of this specification is not implied. exposure to absolute maximum rating conditions for extended periods may affect device reliability. storage temperature ................................ ..... -65c to +150c voltage on any pin except reset with respect to ground ............................. ...-1.0v to v cc +0.5v voltage on reset with respect to ground......-1.0v to +13.0v maximum operating voltage .......................... .................. 6.0v dc current per i/o pin ............................. .................. 40.0 ma dc current v cc and gnd pins................................ 200.0 ma dc characteristics t a = -40 c to 125 c, v cc = 2.7v to 5.5v (unless otherwise noted) (1) . symbol parameter condition min. typ. max. units v il input low voltage except reset and xtal pins -0.5 0.3v cc v v il1 input low voltage xtal pin -0.5 0.1v cc v v il2 input low voltage reset pin -0.5 0.1v cc v v ih input high-voltage except reset and xtal pins 0.6v cc (3) v cc +0.5 v v ih1 input high-voltage xtal pin 0.9v cc (3) v cc +0.5 v v ih2 input high-voltage reset pin 0.9v cc (3) v cc +0.5 v v ol output low voltage (4) (port b) except pb5 i ol = 8 ma, v cc = 5v i ol = 5 ma, v cc = 3v 0.6 0.5 vv v oh output high-voltage (5) (port b) except pb5 i oh = -8 ma, v cc = 5v i oh = -5 ma, v cc = 3v 4.1 2.3 vv v ol1 output low voltage (4) pb5 i ol = 1 ma 0.6 v v oh1 output high-voltage (5) pb5 i oh = -200a, v cc = 5v 3.2 v i il input leakage current i/o pin except reset vcc = 5.5 v, pin low (absolute value) 1 a i ih input leakage current i/o pin except reset vcc = 5.5 v, pin high (absolute value) 1 a r rst reset pull-up resistor 30 60 k r pu i/o pin pull-up resistor 20 50 k
157 7598d?avr?02/07 attiny25/45/85 auto notes: 1. all dc characteristics contained in this da ta sheet result from actual silicon characterizatio n. 2. ?max? means the highest value where the pin is gu aranteed to be read as low. 3. ?min? means the lowest value where the pin is gua ranteed to be read as high. 4. although each i/o port can sink more than the tes t conditions (8 ma at v cc = 5v, 5 ma at v cc = 3v) under steady state con- ditions (non-transient), the following must be obse rved: 1] the sum of all iol, for all ports, should not ex ceed 60 ma. if iol exceeds the test condition, vol may exceed t he related specification. pins are not guaranteed t o sink current greater than the listed test condition. 5. although each i/o port can source more than the t est conditions (8 ma at v cc = 5v, 5 ma at v cc = 3v) under steady state conditions (non-transient), the following must be o bserved: 1] the sum of all ioh, for all ports, should not ex ceed 60 ma. if ioh exceeds the test condition, voh may exceed t he related specification. pins are not guaranteed t o source current greater than the listed test condition. 23.2 external clock drive waveforms figure 23-1. external clock drive waveforms i cc power supply current active 4mhz, v cc = 3v 1.25 2 ma active 8mhz, v cc = 5v 5 10 ma active 16mhz, v cc = 5v 10 15 ma idle 4mhz, v cc = 3v 0.4 0.5 ma idle 8mhz, v cc = 5v 1.2 2 ma idle 16mhz, v cc = 5v 2.5 5 ma power-down mode wdt enabled, v cc = 3v 5 30 a wdt disabled, v cc = 3v 2 24 a wdt enabled, v cc = 5v 9 50 a wdt disabled, v cc = 5v 3 36 a dc characteristics t a = -40 c to 125 c, v cc = 2.7v to 5.5v (unless otherwise noted) (1) . symbol parameter condition min. typ. max. units v il1 v ih1
158 7598d?avr?02/07 attiny25/45/85 auto 23.3 external clock drive notes: 1. all dc characteristics contained in this da ta sheet result from actual silicon characterizatio n. figure 23-2. maximum frequency vs. v cc table 23-1. external clock drive (1) . preliminary symbol parameter v cc = 2.7 - 5.5v v cc = 4.5 - 5.5v units min. max. min. max. 1/t clcl clock frequency 0 8 0 16 mhz t clcl clock period 100 50 ns t chcx high time 40 20 ns t clcx low time 40 20 ns t clch rise time 1.6 0.5 s t chcl fall time 1.6 0.5 s ? t clcl change in period from one clock cycle to the next 2 2 % 16 mhz8 mhz 2.7v 4.5v 5.5v safe operating area
159 7598d?avr?02/07 attiny25/45/85 auto 23.4 adc characteristics ? preliminary data note: 1. all dc characteristics contained in this dat a sheet result from actual silicon characterization . 2. minimum for avcc is 2.7v. 3. maximum for avcc is 5.5v. table 23-2. adc characteristics, single ended channels. -40 c - 125 c. (1) . preliminary symbol parameter condition min (1) typ (1) max (1) units resolution single ended conversion 10 bits absolute accuracy (including inl, dnl, quantization error, gain and offset error) single ended conversion v ref = 4v, v cc = 4v, adc clock = 200 khz 2 lsb single ended conversion v ref = 4v, v cc = 4v, adc clock = 1 mhz 3 lsb single ended conversion v ref = 4v, v cc = 4v, adc clock = 200 khz noise reduction mode 1.5 lsb single ended conversion v ref = 4v, v cc = 4v, adc clock = 1 mhz noise reduction mode 2.5 lsb integral non-linearity (inl) single ended conversion v ref = 4v, v cc = 4v, adc clock = 200 khz 1 lsb differential non-linearity (dnl) single ended conversion v ref = 4v, v cc = 4v, adc clock = 200 khz 0.5 lsb gain error single ended conversion v ref = 4v, v cc = 4v, adc clock = 200 khz 2.5 lsb offset error single ended conversion v ref = 4v, v cc = 4v, adc clock = 200 khz 1.5 lsb conversion time free running conversion 13 260 s clock frequency 50 1000 khz avcc analog supply voltage v cc - 0.3 (2) v cc + 0.3 (3) v v in input voltage gnd v ref -50mv v input bandwidth 38.5 khz v int internal voltage reference 1.0 1.1 1.2 v r ain analog input resistance 100 m
160 7598d?avr?02/07 attiny25/45/85 auto 23.5 calibrated rc oscillator accuracy 24. typical characteristics the data contained in this section is extracted fro m actual silicon characterization and should be treated as indications of how the part will behave. the following charts show typical behavior. these f igures are not tested during manufacturing. all current consumption measurements are performed with all i/o pins configured as inputs and with internal pull-ups enabled. a sine wave generat or with railtorail output is used as clock source. the power consumption in power-down mode is indepen dent of clock selection. the current consumption is a function of several fa ctors such as: operating voltage, operating frequency, loading of i/o pins, switching rate of i /o pins, code executed and ambient tempera- ture. the dominating factors are operating voltage and frequency. the current drawn from capacitive loaded pins may b e estimated (for one pin) as cl*vcc*f where cl = load capacitance, vcc = operating voltag e and f = average switching frequency of i/o pin. the parts are characterized at frequencies higher t han test limits. parts are not guaranteed to function properly at frequencies higher than the or dering code indicates. the difference between current consumption in power -down mode with watchdog timer enabled and power-down mode with watchdog timer dis abled represents the differential cur- rent drawn by the watchdog timer. table 23-3. calibration accuracy of internal rc oscillator frequency vcc temperature calibration accuracy factory calibration 8.0 mhz 3v 25c 1% user calibration 7.3 - 8.1 mhz 2.7v - 5.5v -40c - +12 5c 14%
161 7598d?avr?02/07 attiny25/45/85 auto 24.1 active supply current figure 24-1. active supply current vs. frequency (0.1 - 1.0 mhz) figure 24-2. active supply current vs. frequency (1 - 20 mhz) active s up p ly current vs . low frequency 0.1 - 1.0 mhz 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0.000 0.005 0.010 0.015 0.020 0.025 0.030 0.035 0.040 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 fre que nc y (mhz ) i cc (ma) active s up p ly current vs . frequency 1 - 20mhz 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 2 4 6 8 10 12 14 0 2 4 6 8 10 12 14 16 18 20 fre que nc y (mhz ) i cc (ma)
162 7598d?avr?02/07 attiny25/45/85 auto figure 24-3. active supply current vs. v cc (internal rc oscillator, 128 khz) figure 24-4. active supply current vs. v cc (internal rc oscillator, 1 mhz) active s up p ly current vs . v c c internal rc oscillator, 128 khz 125 ?c 85 ?c 25 ?c -40 ?c 0 0.05 0.1 0.15 0.2 0.25 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma) 125 ?c 85 ?c 25 ?c -40 ?c active s up p ly current vs . v c c internal rc oscillator, 1 mhz 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma)
163 7598d?avr?02/07 attiny25/45/85 auto figure 24-5. active supply current vs. v cc (internal rc oscillator, 8 mhz) 24.2 idle supply current figure 24-6. idle supply current vs. frequency (0.1 - 1.0 mhz) active s up p ly current vs . v c c internal rc oscillator, 8 mhz 0 1 2 3 4 5 6 7 8 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma) 125 ?c 85 ?c 25 ?c -40 ?c idle s up p ly current vs . low frequency 0.1 - 1.0 mhz 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0.05 0.1 0.15 0.2 0.25 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 fre que nc y (mhz ) idle (ma)
164 7598d?avr?02/07 attiny25/45/85 auto figure 24-7. idle supply current vs. frequency (1 - 20 mhz) figure 24-8. idle supply current vs. v cc (internal rc oscillator, 128 khz) idle s up p ly current vs . frequency 1 - 20mhz 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0.5 1 1.5 2 2.5 3 3.5 4 0 2 4 6 8 10 12 14 16 18 20 fre que nc y (mhz ) idle (ma) idle s up p ly current vs . v c c internal rc oscillator, 128 khz 125 ?c 85 ?c 25 ?c -40 ?c 0 0.05 0.1 0.15 0.2 0.25 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc
165 7598d?avr?02/07 attiny25/45/85 auto figure 24-9. idle supply current vs. v cc (internal rc oscillator, 1 mhz) figure 24-10. idle supply current vs. v cc (internal rc oscillator, 8 mhz) 24.2.1 using the power reduction register the tables and formulas below can be used to calcul ate the additional current consumption for the different i/o modules in active and idle mode. the enabling or disabling of the i/o modules 125 ?c 85 ?c 25 ?c -40 ?c idle s up p ly current vs . v c c internal rc oscillator, 1 mhz 0 0.1 0.2 0.3 0.4 0.5 0.6 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) idle (ma) idle s up p ly current vs . v c c internal rc oscillator, 8 mhz 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) idle (ma) 125 ?c 85 ?c 25 ?c -40 ?c
166 7598d?avr?02/07 attiny25/45/85 auto are controlled by the power reduction register. see ?power reduction register? on page 33 for details. it is possible to calculate the typical current con sumption based on the numbers from table 2 for other v cc and frequency settings than listed in table 1. 24.2.1.1 example 1 calculate the expected current consumption in idle mode with usi, timer0, and adc enabled at v cc = 2.0v and f = 1mhz. from table 24-2 , third column, we see that we need to add 6.4% for the usi, 7.3% for the timer0 module, and 21.4% for the adc module. reading from figure 24-9 , we find that the idle current consumption is ~0,2 5ma at v cc = 3.0v and f = 1mhz. the total current consumption in idle mode with usi, ti mer0, and adc enabled, gives: table 24-1. additional current consumption for the different i/ o modules (absolute values) prr bit typical numbers v cc = 2v, f = 1mhz v cc = 3v, f = 4mhz v cc = 5v, f = 8mhz prtim1 43 ua 270 ua 1090 ua prtim0 5.0 ua 28 ua 116 ua prusi 4.0 ua 25 ua 102 ua pradc 13 ua 84 ua 351 ua table 24-2. additional current consumption (percentage) in acti ve and idle mode prr bit additional current consumption compared to active with external clock (see figure 24-1 and figure 24-2 ) additional current consumption compared to idle with external clock (see figure 24-6 and figure 24-7 ) prtim1 17.3% 68.4 % prtim0 1.8 % 7.3 % prusi 1.6 % 6.4 % pradc 5.4 % 21.4 % i cc total 0.25 ( ) ma 1 0.064 0.073 0.214 + + + ( ) ? 0.337 ma
167 7598d?avr?02/07 attiny25/45/85 auto 24.3 power-down supply current figure 24-11. power-down supply current vs. v cc (watchdog timer disabled) figure 24-12. power-down supply current vs. v cc (watchdog timer enabled) p ower-down s up p ly current vs . v c c watchdog timer disabled 125 ?c 85 ?c 25 ?c -40 ?c 0 0.5 1 1.5 2 2.5 3 3.5 4 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ua) p ower-down s up p ly current vs . v c c watchdog timer enabled 125 ?c 85 ?c 25 ?c -40 ?c 0 2 4 6 8 10 12 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ua)
168 7598d?avr?02/07 attiny25/45/85 auto 24.4 pin pull-up figure 24-13. i/o pin pull-up resistor current vs. input voltage (v cc = 1.8v) figure 24-14. i/o pin pull-up resistor current vs. input voltage (v cc = 2.7v) i/o p in p ull-up res is tor current vs . inp ut voltage vc c = 1.8v 125 ?c 85 ?c 25 ?c -40 ?c 0 10 20 30 40 50 60 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 v op (v) i op (ua) i/o p in p ull-up res is tor current vs . inp ut voltage vc c = 2.7v 125 ?c 85 ?c 25 ?c -40 ?c 0 10 20 30 40 50 60 70 80 90 0 0.5 1 1.5 2 2.5 3 v op (v) i op (ua)
169 7598d?avr?02/07 attiny25/45/85 auto figure 24-15. i/o pin pull-up resistor current vs. input voltage (v cc = 5.0v) figure 24-16. reset pull-up resistor current vs. reset pin voltag e (v cc = 1.8v) i/o p in p ull-up res is tor current vs . inp ut voltage vc c = 5.0v -40 ?c 85 ?c 25 ?c 125 ?c 0 20 40 60 80 100 120 140 160 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 v op (v) i op (ua) res et p ull-up res is tor current vs . res et p in voltag e vc c = 1.8v 125 ?c 85 ?c 25 ?c -40 ?c 0 5 10 15 20 25 30 35 40 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 v re s e t (v) i re s e t (ua)
170 7598d?avr?02/07 attiny25/45/85 auto figure 24-17. reset pull-up resistor current vs. reset pin voltag e (v cc = 2.7v) figure 24-18. reset pull-up resistor current vs. reset pin voltag e (v cc = 5.0v) res et p ull-up res is tor current vs . res et p in voltag e vc c = 2.7v 0 10 20 30 40 50 60 70 0 0.5 1 1.5 2 2.5 3 v re s e t (v) i re s e t (ua) 125 ?c 85 ?c 25 ?c -40 ?c 125 ?c 85 ?c 25 ?c -40 ?c res et p ull-up res is tor current vs . res et p in voltag e vc c = 5.0v 0 20 40 60 80 100 120 140 0 1 2 3 4 5 6 v re s e t (v) i re s e t (ua)
171 7598d?avr?02/07 attiny25/45/85 auto 24.5 pin driver strength figure 24-19. i/o pin source current vs. output voltage (v cc = 1.8v) figure 24-20. i/o pin source current vs. output voltage (v cc = 3v) i/o p in s ink current vs . outp ut voltage v cc = 1.8v 125 ?c 85 ?c 25 ?c -40 ?c 0 2 4 6 8 10 12 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 v ol (v) i ol (ma) i/o p in outp ut voltage vs . s ink current vc c = 3.0v 125 85 25 -40 0 0.2 0.4 0.6 0.8 1 1.2 0 5 10 15 20 25 i ol (v) v ol (v)
172 7598d?avr?02/07 attiny25/45/85 auto figure 24-21. i/o pin source current vs. output voltage (v cc = 5v) figure 24-22. i/o pin sink current vs. output voltage (v cc = 1.8v) i/o p in outp ut voltage vs . s ink current vc c = 5.0v 125 85 25 -40 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0 5 10 15 20 25 i ol (v) v ol (v) i/o p in s ource current vs . outp ut voltage v cc = 1.8v 125 ?c 85 ?c 25 ?c -40 ?c 0 1 2 3 4 5 6 7 8 9 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 v oh (v) i oh (ma)
173 7598d?avr?02/07 attiny25/45/85 auto figure 24-23. i/o pin sink current vs. output voltage (v cc = 3v) figure 24-24. i/o pin sink current vs. output voltage (v cc = 5.0v) i/o p in outp ut voltage vs . s ource current vc c = 3v 125 85 25 -40 0 0.5 1 1.5 2 2.5 3 3.5 0 5 10 15 20 25 i oh (ma) v oh ( v i/o p in outp ut voltage vs . s ource current vc c = 5.0v 125 85 25 -40 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5 5.1 0 5 10 15 20 25 i oh (ma) v oh (v)
174 7598d?avr?02/07 attiny25/45/85 auto 24.6 pin thresholds and hysteresis figure 24-25. i/o pin input threshold voltage vs. v cc (vih, i/o pin read as '1') figure 24-26. i/o pin input threshold voltage vs. v cc (vil, i/o pin read as '0') i/o p in inp ut thres hold voltage vs . v c c vih, io pin read as '1' 0 0.5 1 1.5 2 2.5 3 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) thre s hold 125 ?c 85 ?c 25 ?c -40 ?c i/o p in inp ut thres hold voltage vs . v c c vil, io pin read as '0' 0 0.5 1 1.5 2 2.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) thre s hold 125 ?c 85 ?c 25 ?c -40 ?c
175 7598d?avr?02/07 attiny25/45/85 auto figure 24-27. i/o pin input hysteresis vs. v cc figure 24-28. reset input threshold voltage vs. v cc (vih, reset pin read as '1') i/o p in inp ut hys teres is 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) thre s hold 125 ?c 85 ?c 25 ?c -40 ?c res et inp ut thres hold voltage vs . v c c vih, io pin read as '1' 0 0.5 1 1.5 2 2.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) thre s hold 125 ?c 85 ?c 25 ?c -40 ?c
176 7598d?avr?02/07 attiny25/45/85 auto figure 24-29. reset input threshold voltage vs. v cc (vil, reset pin read as '0') figure 24-30. reset input pin hysteresis vs. v cc res et inp ut thres hold voltage vs . v c c vil, io pin read as '0' 0 0.5 1.5 2 2.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) thre s hold 125 ?c 85 ?c 25 ?c -40 ?c 125 ?c 85 ?c 25 ?c -40 ?c res et inp ut thres hold voltage vs . v c c vih, io pin read as '1' 0 0.05 0.1 0.15 0.2 0.25 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) thre s hold
177 7598d?avr?02/07 attiny25/45/85 auto 24.7 bod thresholds and analog comparator offset figure 24-31. bod thresholds vs. temperature (bodlevel is 4.3v) figure 24-32. bod thresholds vs. temperature (bodlevel is 2.7v) bod thres holds vs . temp erature bodlevel = 4.3v ris ing falling 4 4.05 4.1 4.15 4.2 4.25 4.3 4.35 4.4 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 te mpe ra ture (c) thre s hold (v) bod thres holds vs . temp erature bodlevel = 2.7v ris ing falling 2.5 2.55 2.6 2.65 2.7 2.75 2.8 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 te mpe ra ture (c) thre s hold (v)
178 7598d?avr?02/07 attiny25/45/85 auto figure 24-33. bod thresholds vs. temperature (bodlevel is 1.8v) 24.8 internal oscillator speed figure 24-34. watchdog oscillator frequency vs. v cc ) bod thres holds vs . temp erature bodlevel a t 1.8v ris ing vcc falling vcc 1.6 1.65 1.7 1.75 1.8 1.85 1.9 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 te mpe ra ture (c) thre s hold (v) watchdoc os cillator frequency vs . v c c 125 ?c 85 ?c 25 ?c -40 ?c 0.1 0.102 0.104 0.106 0.108 0.11 0.112 0.114 0.116 0.118 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) f rc (mhz )
179 7598d?avr?02/07 attiny25/45/85 auto figure 24-35. watchdog oscillator frequency vs. temperature figure 24-36. calibrated 8 mhz rc oscillator frequency vs. temper ature watchdog os cillator frequency vs . temp erature 5.5 v 4.0 v 3.6 v 2.7 v 1.8 v 0.1 0.102 0.104 0.106 0.108 0.11 0.112 0.114 0.116 0.118 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 te mpe ra ture f rc (mhz ) calibrated 8mhz rc os cillator frequency vs . temp era ture 5.0 v 3.0 v 7.5 7.6 7.7 7.8 7.9 8 8.1 8.2 8.3 8.4 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 te mpe ra ture f rc (mhz )
180 7598d?avr?02/07 attiny25/45/85 auto figure 24-37. calibrated 8 mhz rc oscillator frequency vs. v cc figure 24-38. calibrated 8 mhz rc oscillator frequency vs. osccal value calibrated 8mhz rc os cillator frequency vs . op erati ng voltage 125 ?c 85 ?c 25 ?c -40 ?c 7.5 7.6 7.7 7.8 7.9 8 8.1 8.2 8.3 8.4 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) f rc (mhz ) calibrated 8mhz rc os cillator frequency vs . os ccal value 125 ?c 85 ?c 25 ?c -40 ?c 2 4 6 8 10 12 14 0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 osccal (x1) f rc (mhz )
181 7598d?avr?02/07 attiny25/45/85 auto 24.9 current consumption of peripheral units figure 24-39. brownout detector current vs. v cc figure 24-40. analog comparator current vs. v cc brownout detector current vs . v c c 125 ?c 85 ?c 25 ?c -40 ?c 0 5 10 15 20 25 30 35 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ua) analog comp arator current vs . v c c aref = avc c 150 ?c 125 ?c 85 ?c 25 ?c -40 ?c 0 50 100 150 200 250 300 350 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 v cc (v) i cc (ua)
182 7598d?avr?02/07 attiny25/45/85 auto 24.10 current consumption in reset and reset pulse w idth figure 24-41. reset supply current vs. v cc (0.1 - 1.0 mhz, excluding current through the reset pull-up) figure 24-42. reset supply current vs. v cc (1 - 24 mhz, excluding current through the reset pull-up) res et s up p ly current vs . v c c 0.1 - 1.0 mhz , excluding current through the reset pullup 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 fre que nc y (mhz ) i cc (ma) res et s up p ly current vs . v c c 1 - 20 mhz , excluding current through the reset pu llup 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0.5 1 1.5 2 2.5 0 2 4 6 8 10 12 14 16 18 20 fre que nc y (mhz ) i cc (ma)
183 7598d?avr?02/07 attiny25/45/85 auto figure 24-43. reset pulse width vs. v cc 24.11 analog to digital converter figure 24-44. analog to digital converter differential mode offse t vs. v cc minimum res et p uls e width vs . v c c 125 ?c 25 ?c 85 ?c -40 ?c 0 500 1000 1500 2000 2500 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) puls e width (ns ) analog to digital converter - offs et diffe re ntia l inputs , vc c = 4v, vre f = 4v diff x1 diff x20 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 te mpe ra ture
184 7598d?avr?02/07 attiny25/45/85 auto figure 24-45. analog to digital converter single endded mode offs et vs. v cc figure 24-46. analog to digital converter differential mode gain vs. v cc analog to digital converter - offs et single ende d, vc c = 4v, vre f = 4v 0 0.5 1 1.5 2 2.5 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 te mpe ra ture lsb analog to digital converter - gain diffe re ntia l inputs , vc c = 5v, vre f = 4v diff x1 diff x20 -3 -2.8 -2.6 -2.4 -2.2 -2 -1.8 -1.6 -1.4 -1.2 -1 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 te mpe ra ture lsb
185 7598d?avr?02/07 attiny25/45/85 auto figure 24-47. analog to digital converter single endded mode gain vs. v cc figure 24-48. analog to digital converter differential mode dnl v s. v cc analog to digital converter - gain single ende d, vc c = 4v, vre f = 4v -2.5 -2 -1.5 -1 -0.5 0 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 te mpe ra ture lsb analog to digital converter - differential non lin earity dnl diffe re ntia l inputs , vc c = 4v, vre f = 4v diff x1 diff x20 0 0.2 0.4 0.6 0.8 1 1.2 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 te mpe ra ture lsb
186 7598d?avr?02/07 attiny25/45/85 auto figure 24-49. analog to digital converter single endded mode dnl vs. v cc figure 24-50. analog to digital converter differential mode inl v s. v cc analog to digital converter - differential non line arity dnl single ende d, vc c = 4v, vre f = 4v 0.47 0.48 0.49 0.5 0.51 0.52 0.53 0.54 0.55 0.56 0.57 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 te mpe ra ture lsb analog to digital converter - integral non linearit y inl diffe re ntia l inputs , vc c = 4v, vre f = 4v diff x1 diff x20 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 te mpe ra ture lsb
187 7598d?avr?02/07 attiny25/45/85 auto figure 24-51. analog to digital converter single endded mode inl vs. v cc analog to digital converter - integral non linearit y inl single ende d, vc c = 4v, vre f = 4v 0.58 0.6 0.62 0.64 0.66 0.68 0.7 0.72 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 te mpe ra ture lsb
188 7598d?avr?02/07 attiny25/45/85 auto 25. register summary address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 page 0x3f sreg i t h s v n z c page 7 0x3e sph ? ? ? ? ? ? ? sp8 page 10 0x3d spl sp7 sp6 sp5 sp4 sp3 sp2 sp1 sp0 page 10 0x3c reserved ? 0x3b gimsk ? int0 pcie ? ? ? ? ? page 59 0x3a gifr ? intf0 pcif ? ? ? ? ? page 59 0x39 timsk ? ocie1a ocie1b ocie0a ocie0b toie1 toie0 ? page 76 0x38 tifr ? ocf1a ocf1b ocf0a ocf0b tov1 tov0 ? page 76 0x37 spmcsr ? ? ? ctpb rflb pgwrt pgers spmen page 137 0x36 reserved ? 0x35 mcucr ? pud se sm1 sm0 ? isc01 isc00 page 31 , page 53 , page 58 0x34 mcusr ? ? ? ? wdrf borf extrf porf page 39 , 0x33 tccr0b foc0a foc0b ? ? wgm02 cs02 cs01 cs00 page 74 0x32 tcnt0 timer/counter0 page 75 0x31 osccal oscillator calibration register page 27 0x30 tccr1 ctc1 pwm1a com1a1 com1a0 cs13 cs12 cs11 cs10 page 82 , page 94 0x2f tcnt1 timer/counter1 page 84 , page 95 0x2e ocr1a timer/counter1 output compare register a page 85 , page 96 0x2d ocr1c timer/counter1 output compare register c page 85 , page 96 0x2c gtccr tsm pwm1b com1b1 com1b0 foc1b foc1a psr1 psr0 page 78 , page 83 , page 95 0x2b ocr1b timer/counter1 output compare register b page 85 0x2a tccr0a com0a1 com0a0 com0b1 com0b0 ? wgm01 wgm00 page 71 0x29 ocr0a timer/counter0 ? output compare register a page 75 0x28 ocr0b timer/counter0 ? output compare register b page 76 0x27 pllcsr sm ? ? ? ? pcke plle plock page 87 , page 97 0x26 clkpr clkpce ? ? ? clkps3 clkps2 clkps1 clkps0 page 29 0x25 dt1a dt1ah3 dt1ah2 dt1ah1 dt1ah0 dt1al3 dt1al2 dt1al1 d t1al0 page 102 0x24 dt1b dt1bh3 dt1bh2 dt1bh1 dt1bh0 dt1bl3 dt1bl2 dt1bl1 dt1bl0 page 102 0x23 dtps1 - - - - - - dtps11 dtps10 page 101 0x22 dwdr dwdr[7:0] page 135 0x21 wdtcr wdtif wdtie wdp3 wdce wde wdp2 wdp1 wdp0 page 41 0x20 prr ? prtim1 prtim0 prusi pradc page 33 0x1f eearh eear8 page 15 0x1e eearl eear7 eear6 eear5 eear4 eear3 eear2 eear1 eear0 page 16 0x1d eedr eeprom data register page 16 0x1c eecr ? ? eepm1 eepm0 eerie eemwe eewe eere page 16 0x1b reserved ? 0x1a reserved ? 0x19 reserved ? 0x18 portb ? ? portb5 portb4 portb3 portb2 portb1 portb0 page 58 0x17 ddrb ? ? ddb5 ddb4 ddb3 ddb2 ddb1 ddb0 page 58 0x16 pinb ? ? pinb5 pinb4 pinb3 pinb2 pinb1 pinb0 page 58 0x15 pcmsk ? ? pcint5 pcint4 pcint3 pcint2 pcint1 pcint0 page 60 0x14 didr0 ? ? adc0d adc2d adc3d adc1d ein1d ain0d page 117 , page 133 0x13 gpior2 general purpose i/o register 2 0x12 gpior1 general purpose i/o register 1 0x11 gpior0 general purpose i/o register 0 0x10 usibr usi buffer register page 110 0x0f usidr usi data register page 110 0x0e usisr usicif usioif usipf usidc usicnt3 usicnt2 usicn t1 usicnt0 page 111 0x0d usicr usisie usioie usiwm1 usiwm0 usics1 usics0 usiclk usitc page 112 0x0c reserved ? 0x0b reserved ? 0x0a reserved ? 0x09 reserved ? 0x08 acsr acd acbg aco aci acie ? acis1 acis0 page 115 0x07 admux refs1 refs0 adlar refs2 mux3 mux2 mux1 mux0 page 128 0x06 adcsra aden adsc adate adif adie adps2 adps1 adps0 page 130 0x05 adch adc data register high byte page 131 0x04 adcl adc data register low byte page 131 0x03 adcsrb bin acme ipr ? ? adts2 adts1 adts0 page 115 , page 132 0x02 reserved ? 0x01 reserved ? 0x00 reserved ?
189 7598d?avr?02/07 attiny25/45/85 auto note: 1. for compatibility with future devices, reser ved bits should be written to zero if accessed. res erved i/o memory addresses should never be written. 2. i/o registers within the address range 0x00 - 0x1 f are directly bit-accessible using the sbi and cbi instructions. in these registers, the value of single bits can be checked by using the sbis and sbic instructions. 3. some of the status flags are cleared by writing a logical one to them. note that, unlike most other avrs, the cbi and sbi instructions will only operation the specified bit, and can therefore be used on registers containing such status flags. the cbi and sbi instructions work with registers 0x00 t o 0x1f only.
190 7598d?avr?02/07 attiny25/45/85 auto 26. instruction set summary mnemonics operands description operation flags #clocks arithmetic and logic instructions add rd, rr add two registers rd rd + rr z,c,n,v,h 1 adc rd, rr add with carry two registers rd rd + rr + c z,c,n,v,h 1 adiw rdl,k add immediate to word rdh:rdl rdh:rdl + k z,c,n,v,s 2 sub rd, rr subtract two registers rd rd - rr z,c,n,v,h 1 subi rd, k subtract constant from register rd rd - k z,c,n,v,h 1 sbc rd, rr subtract with carry two registers rd rd - rr - c z,c,n,v,h 1 sbci rd, k subtract with carry constant from reg. rd rd - k - c z,c,n,v,h 1 sbiw rdl,k subtract immediate from word rdh:rdl rdh:rdl - k z,c,n,v,s 2 and rd, rr logical and registers rd rd ? rr z,n,v 1 andi rd, k logical and register and constant rd rd ? k z,n,v 1 or rd, rr logical or registers rd rd v rr z,n,v 1 ori rd, k logical or register and constant rd rd v k z,n,v 1 eor rd, rr exclusive or registers rd rd rr z,n,v 1 com rd one?s complement rd 0xff ? rd z,c,n,v 1 neg rd two?s complement rd 0x00 ? rd z,c,n,v,h 1 sbr rd,k set bit(s) in register rd rd v k z,n,v 1 cbr rd,k clear bit(s) in register rd rd ? (0xff - k) z,n,v 1 inc rd increment rd rd + 1 z,n,v 1 dec rd decrement rd rd ? 1 z,n,v 1 tst rd test for zero or minus rd rd ? rd z,n,v 1 clr rd clear register rd rd rd z,n,v 1 ser rd set register rd 0xff none 1 branch instructions rjmp k relative jump pc pc + k + 1 none 2 ijmp indirect jump to (z) pc z none 2 rcall k relative subroutine call pc pc + k + 1 none 3 icall indirect call to (z) pc z none 3 ret subroutine return pc stack none 4 reti interrupt return pc stack i 4 cpse rd,rr compare, skip if equal if (rd = rr) pc pc + 2 or 3 none 1/2/3 cp rd,rr compare rd ? rr z, n,v,c,h 1 cpc rd,rr compare with carry rd ? rr ? c z, n,v,c,h 1 cpi rd,k compare register with immediate rd ? k z, n,v,c,h 1 sbrc rr, b skip if bit in register cleared if (rr(b)=0) pc pc + 2 or 3 none 1/2/3 sbrs rr, b skip if bit in register is set if (rr(b)=1) pc pc + 2 or 3 none 1/2/3 sbic p, b skip if bit in i/o register cleared if (p(b)=0) pc pc + 2 or 3 none 1/2/3 sbis p, b skip if bit in i/o register is set if (p(b)=1) pc pc + 2 or 3 none 1/2/3 brbs s, k branch if status flag set if (sreg(s) = 1) then pc pc+k + 1 none 1/2 brbc s, k branch if status flag cleared if (sreg(s) = 0) then pc pc+k + 1 none 1/2 breq k branch if equal if (z = 1) then pc pc + k + 1 none 1/2 brne k branch if not equal if (z = 0) then pc pc + k + 1 none 1/2 brcs k branch if carry set if (c = 1) then pc pc + k + 1 none 1/2 brcc k branch if carry cleared if (c = 0) then pc pc + k + 1 none 1/2 brsh k branch if same or higher if (c = 0) then pc pc + k + 1 none 1/2 brlo k branch if lower if (c = 1) then pc pc + k + 1 none 1/2 brmi k branch if minus if (n = 1) then pc pc + k + 1 none 1/2 brpl k branch if plus if (n = 0) then pc pc + k + 1 none 1/2 brge k branch if greater or equal, signed if (n v= 0) then pc pc + k + 1 none 1/2 brlt k branch if less than zero, signed if (n v= 1) then pc pc + k + 1 none 1/2 brhs k branch if half carry flag set if (h = 1) then pc pc + k + 1 none 1/2 brhc k branch if half carry flag cleared if (h = 0) then pc pc + k + 1 none 1/2 brts k branch if t flag set if (t = 1) then pc pc + k + 1 none 1/2 brtc k branch if t flag cleared if (t = 0) then pc pc + k + 1 none 1/2 brvs k branch if overflow flag is set if (v = 1) then pc pc + k + 1 none 1/2 brvc k branch if overflow flag is cleared if (v = 0) then pc pc + k + 1 none 1/2 brie k branch if interrupt enabled if ( i = 1) then pc pc + k + 1 none 1/2 brid k branch if interrupt disabled if ( i = 0) then pc pc + k + 1 none 1/2 bit and bit-test instructions sbi p,b set bit in i/o register i/o(p,b) 1 none 2 cbi p,b clear bit in i/o register i/o(p,b) 0 none 2 lsl rd logical shift left rd(n+1) rd(n), rd(0) 0 z,c,n,v 1 lsr rd logical shift right rd(n) rd(n+1), rd(7) 0 z,c,n,v 1 rol rd rotate left through carry rd(0) c,rd(n+1) rd(n),c rd(7) z,c,n,v 1
191 7598d?avr?02/07 attiny25/45/85 auto ror rd rotate right through carry rd(7) c,rd(n) rd(n+1),c rd(0) z,c,n,v 1 asr rd arithmetic shift right rd(n) rd(n+1), n=0..6 z,c,n,v 1 swap rd swap nibbles rd(3..0) rd(7..4),rd(7..4) rd(3..0) none 1 bset s flag set sreg(s) 1 sreg(s) 1 bclr s flag clear sreg(s) 0 sreg(s) 1 bst rr, b bit store from register to t t rr(b) t 1 bld rd, b bit load from t to register rd(b) t none 1 sec set carry c 1 c 1 clc clear carry c 0 c 1 sen set negative flag n 1 n 1 cln clear negative flag n 0 n 1 sez set zero flag z 1 z 1 clz clear zero flag z 0 z 1 sei global interrupt enable i 1 i 1 cli global interrupt disable i 0 i 1 ses set signed test flag s 1 s 1 cls clear signed test flag s 0 s 1 sev set twos complement overflow. v 1 v 1 clv clear twos complement overflow v 0 v 1 set set t in sreg t 1 t 1 clt clear t in sreg t 0 t 1 seh set half carry flag in sreg h 1 h 1 clh clear half carry flag in sreg h 0 h 1 data transfer instructions mov rd, rr move between registers rd rr none 1 movw rd, rr copy register word rd+1:rd rr+1:rr none 1 ldi rd, k load immediate rd k none 1 ld rd, x load indirect rd (x) none 2 ld rd, x+ load indirect and post-inc. rd (x), x x + 1 none 2 ld rd, - x load indirect and pre-dec. x x - 1, rd (x) none 2 ld rd, y load indirect rd (y) none 2 ld rd, y+ load indirect and post-inc. rd (y), y y + 1 none 2 ld rd, - y load indirect and pre-dec. y y - 1, rd (y) none 2 ldd rd,y+q load indirect with displacement rd (y + q) none 2 ld rd, z load indirect rd (z) none 2 ld rd, z+ load indirect and post-inc. rd (z), z z+1 none 2 ld rd, -z load indirect and pre-dec. z z - 1, rd (z) none 2 ldd rd, z+q load indirect with displacement rd (z + q) none 2 lds rd, k load direct from sram rd (k) none 2 st x, rr store indirect (x) rr none 2 st x+, rr store indirect and post-inc. (x) rr, x x + 1 none 2 st - x, rr store indirect and pre-dec. x x - 1, (x) rr none 2 st y, rr store indirect (y) rr none 2 st y+, rr store indirect and post-inc. (y) rr, y y + 1 none 2 st - y, rr store indirect and pre-dec. y y - 1, (y) rr none 2 std y+q,rr store indirect with displacement (y + q) rr none 2 st z, rr store indirect (z) rr none 2 st z+, rr store indirect and post-inc. (z) rr, z z + 1 none 2 st -z, rr store indirect and pre-dec. z z - 1, (z) rr none 2 std z+q,rr store indirect with displacement (z + q) rr none 2 sts k, rr store direct to sram (k) rr none 2 lpm load program memory r0 (z) none 3 lpm rd, z load program memory rd (z) none 3 lpm rd, z+ load program memory and post-inc rd (z), z z+1 none 3 spm store program memory (z) r1:r0 none in rd, p in port rd p none 1 out p, rr out port p rr none 1 push rr push register on stack stack rr none 2 pop rd pop register from stack rd stack none 2 mcu control instructions nop no operation none 1 sleep sleep (see specific descr. for sleep function) none 1 wdr watchdog reset (see specific descr. for wdr/timer) none 1 break break for on-chip debug only none n/a mnemonics operands description operation flags #clocks
192 7598d?avr?02/07 attiny25/45/85 auto 27. ordering information notes: 1. green and rohs packaging 2. tape and reel with dry-pack delivery. 3. for speed vs. v cc ,see figure 23-2 on page 158 . power supply speed (mhz) ordering code package operatio n range 2.7 - 5.5v 8 - 16 (3) attiny25/45/85-15sz attiny25/45/85-15st attiny25/45/85-15st1 t5 automotive (-40 c to 125 c) automotive (-40c to +85c) automotive (-40c to +105c) 2.7 - 5.5v 8 - 16 (3) attiny25/45/85-15mz attiny25/45/85-15mt attiny25/45/85-15mt1 pc automotive (-40 c to 125 c) automotive (-40c to +85c) automotive (-40c to +105c) package type t5 8-lead, 0.208 pc 20-lead, qfn
193 7598d?avr?02/07 attiny25/45/85 auto 28. packaging information 28.1 t5
194 7598d?avr?02/07 attiny25/45/85 auto 28.2 pc
195 7598d?avr?02/07 attiny25/45/85 auto 29. document revision history 29.1 rev. 7598d - 02/07 1. clarification of power on reset specifications ta ble, table 8-1 on page 37 . 2. errata list updated. 3. added qfn packages. 29.2 rev. 7598c - 09/06 1. correction of package codification and drawings. 29.3 rev. 7598b - 08/06 1. clarification of several tbd values 2. addition of the power on reset specification 3. dc characteristics limits completed after corner run characterization 4. typical characteristic curves produced 29.4 changes from rev. 2535a-09/01 to rev. 7598a-04/06 1. automotive grade created: features: - change voltage and temperature range (2.7v - 5.5v ), (-40c, +125c) - adapt stand-by current to automotive temperature range packages: - pdip removed - ordering info limited to automotive versions (gre en only, dry pack) dc & ac parameters - only preliminary values are produced. typical characteristics removed.
196 7598d?avr?02/07 attiny25/45/85 auto 30. errata the revision letter in this section refers to the r evision of the attiny25/45/85 device. 30.1 attiny25 rev. e 1. no known errata. flash security improvements. 30.2 attiny45 rev. g 1. no known errata. flash security improvements. 30.3 attiny85 rev. c 1. no known errata. flash security improvements.
i 7598d?avr?02/07 auto features ........................................... ................................................... ....... 1 1 pin configurations ............................... ................................................... . 2 2 overview ......................................... ................................................... ....... 2 2.1 block diagram ............................... ................................................... ......................3 2.2 automotive quality grade .................... ................................................... ...............4 2.3 pin descriptions ............................ ................................................... ......................5 3 about code examples .............................. ............................................... 5 4 avr cpu core .................................................. ........................................ 5 4.1 introduction ................................ ................................................... ..........................5 4.2 architectural overview ...................... ................................................... ..................6 4.3 alu ? arithmetic logic unit ................. ................................................... ...............7 4.4 status register ............................. ................................................... .......................7 4.5 general purpose register file ............... ................................................... .............9 4.6 stack pointer ............................... ................................................... ......................10 4.7 instruction execution timing ................ ................................................... .............10 4.8 reset and interrupt handling ................ ................................................... ............11 5 avr attiny25/45/85 memories ...................... ....................................... 13 5.1 in-system re-programmable flash program memor y .........................................13 5.2 sram data memory ............................ ................................................... .............14 5.3 eeprom data memory .......................... ................................................... ..........15 5.4 i/o memory .................................. ................................................... ......................21 6 system clock and clock options ................... ...................................... 21 6.1 clock systems and their distribution ........ ................................................... .........21 6.2 clock sources ............................... ................................................... ....................23 6.3 default clock source ........................ ................................................... .................24 6.4 crystal oscillator .......................... ................................................... .....................24 6.5 low-frequency crystal oscillator ............ ................................................... ...........25 6.6 calibrated internal rc oscillator ........... ................................................... ............26 6.7 external clock .............................. ................................................... .....................27 6.8 128 khz internal oscillator ................. ................................................... ...............28 6.9 clock output buffer ......................... ................................................... ..................29 6.10 system clock prescaler ..................... ................................................... .............29 7 power management and sleep modes ................. ................................ 31 7.1 idle mode ................................... ................................................... ........................32
ii 7598d?avr?02/07 auto 7.2 adc noise reduction mode .................... ................................................... .........32 7.3 power-down mode ............................. ................................................... ...............32 7.4 power reduction register .................... ................................................... .............33 7.5 minimizing power consumption ................ ................................................... ........34 8 system control and reset ......................... ........................................... 35 8.1 internal voltage reference .................. ................................................... ..............40 8.2 watchdog timer .............................. ................................................... ..................40 8.3 timed sequences for changing the configuratio n of the watchdog timer .........44 9 interrupts ....................................... ................................................... ...... 45 9.1 interrupt vectors in attiny25/45/85 ......... ................................................... ..........45 10 i/o ports ....................................... ................................................... ........ 46 10.1 introduction ............................... ................................................... .......................46 10.2 ports as general digital i/o ............... ................................................... ..............47 10.3 alternate port functions ................... ................................................... ...............52 10.4 register description for i/o-ports ......... ................................................... ...........57 11 external interrupts ............................. ................................................... . 58 12 8-bit timer/counter0 with pwm ................... ......................................... 60 12.1 overview ................................... ................................................... ......................60 12.2 timer/counter clock sources ................ ................................................... .........62 12.3 counter unit ............................... ................................................... .....................62 12.4 output compare unit ........................ ................................................... ..............63 12.5 compare match output unit .................. ................................................... ..........64 12.6 modes of operation ......................... ................................................... ................65 12.7 timer/counter timing diagrams .............. ................................................... .......70 12.8 8-bit timer/counter register description ... ................................................... .....71 13 timer/counter prescaler ......................... .............................................. 77 14 8-bit timer/counter1 ............................ .................................................. 7 9 14.1 timer/counter1 ............................. ................................................... ..................80 15 8-bit timer/counter1 in attiny15 mode ........... .................................... 90 15.1 timer/counter1 prescaler ................... ................................................... ............91 15.2 timer/counter1 ............................. ................................................... ..................91 16 dead time generator ............................. .............................................. 100 17 universal serial interface ? usi ................ .......................................... 103
iii 7598d?avr?02/07 auto 17.1 overview ................................... ................................................... ....................103 17.2 functional descriptions .................... ................................................... .............104 17.3 alternative usi usage ...................... ................................................... .............109 17.4 usi register descriptions .................. ................................................... ...........110 18 analog comparator ............................... .............................................. 114 18.1 analog comparator multiplexed input ........ ................................................... ...116 19 analog to digital converter ..................... ........................................... 117 19.1 features ................................... ................................................... .....................117 19.2 operation .................................. ................................................... .....................118 19.3 starting a conversion ...................... ................................................... ..............119 19.4 prescaling and conversion timing ........... ................................................... .....120 19.5 changing channel or reference selection .... ..................................................1 22 19.6 adc noise canceler ......................... ................................................... ............123 19.7 adc conversion result ...................... ................................................... ..........126 20 debugwire on-chip debug system .................. ................................ 133 20.1 features ................................... ................................................... .....................133 20.2 overview ................................... ................................................... ....................133 20.3 physical interface ......................... ................................................... .................134 20.4 software break points ...................... ................................................... .............134 20.5 limitations of debugwire ................... ................................................... ..........135 20.6 debugwire related register in i/o memory ... ................................................135 21 self-programming the flash ...................... ......................................... 135 21.1 addressing the flash during self-programming ..............................................136 22 memory programming .............................. ........................................... 139 22.1 program and data memory lock bits .......... ................................................... .139 22.2 fuse bytes ................................. ................................................... ...................140 22.3 signature bytes ............................ ................................................... .................142 22.4 calibration byte ........................... ................................................... ..................142 22.5 page size .................................. ................................................... ....................142 22.6 serial downloading ......................... ................................................... ..............143 22.7 high-voltage serial programming ............ ................................................... ......148 22.8 high-voltage serial programming algorithm .. ..................................................1 49 22.9 high-voltage serial programming characterist ics ............................................155 23 electrical characteristics ...................... .............................................. 156
iv 7598d?avr?02/07 auto 23.1 absolute maximum ratings* .................. ................................................... .......156 23.2 external clock drive waveforms ............. ................................................... ......157 23.3 external clock drive ....................... ................................................... ...............158 23.4 adc characteristics ? preliminary data ..... ................................................... ...159 23.5 calibrated rc oscillator accuracy .......... ................................................... ......160 24 typical characteristics ......................... ............................................... 160 24.1 active supply current ...................... ................................................... ..............161 24.2 idle supply current ........................ ................................................... ................163 24.3 power-down supply current .................. ................................................... .......167 24.4 pin pull-up ................................ ................................................... .....................168 24.5 pin driver strength ........................ ................................................... ................171 24.6 pin thresholds and hysteresis .............. ................................................... ........174 24.7 bod thresholds and analog comparator offset .............................................177 24.8 internal oscillator speed .................. ................................................... .............178 24.9 current consumption of peripheral units .... ................................................... ..181 24.10 current consumption in reset and reset puls e width ..................................182 24.11 analog to digital converter ............... ................................................... ..........183 25 register summary ................................ ............................................... 188 26 instruction set summary ......................... ........................................... 190 27 ordering information ............................ ............................................... 192 28 packaging information ........................... ............................................. 193 28.1 t5 ......................................... ................................................... .........................193 28.2 pc ......................................... ................................................... ........................194 29 document revision history ....................... ......................................... 195 29.1 rev. 7598d - 02/07 ......................... ................................................... ..............195 29.2 rev. 7598c - 09/06 ......................... ................................................... ..............195 29.3 rev. 7598b - 08/06 ......................... ................................................... ..............195 29.4 changes from rev. 2535a-09/01 to rev. 7598a- 04/06......................................195 30 errata .......................................... ................................................... ........ 196 30.1 attiny25 rev. e ............................ ................................................... ................196 30.2 attiny45 rev. g ............................ ................................................... ................196 30.3 attiny85 rev. c ............................ ................................................... ................196
printed on recycled paper. 7598d?avr?02/07 ? atmel corporation 2007 . all rights reserved. atmel ? , logo and combinations thereof, and everywhere you are ? are the trademarks or regis- tered trademarks, of atmel corporation or its subsi diaries. other terms and product names may be trade marks of others. disclaimer: the information in this document is provided in co nnection with atmel products. no license, express o r implied, by estoppel or otherwise, to any intellectual property right is granted by this docu ment or in connection with the sale of atmel produc ts. except as set forth in atmel?s terms and condi- tions of sale located on atmel?s web site, atmel as sumes no liability whatsoever and disclaims any exp ress, implied or statutory warranty relating to its products including, but no t limited to, the implied warranty of merchantabili ty, fitness for a particular purpose, or non-infringement. in no event shall atm el be liable for any direct, indirect, consequentia l, punitive, special or inciden- tal damages (including, without limitation, damages for loss of profits, business interruption, or los s of information) arising out of the use or inability to use this document, even if atmel has been advised of the possibility of suc h damages. atmel makes no representations or warranties with respect to the a ccuracy or completeness of the contents of this doc ument and reserves the right to make changes to spe cifications and product descriptions at any time without notice . atmel does not make any commitment to update the information contained herein. unless specifically p rovidedot- herwise, atmel products are not suitable for, and s hall not be used in, automotive applications. atmel ?s products are not intended, authorized, or warran ted for use as compo- nents in applications intended to support or sustai n life. atmel corporation atmel operations 2325 orchard parkway san jose, ca 95131, usa tel: 1(408) 441-0311 fax: 1(408) 487-2600 regional headquarters europe atmel sarl route des arsenaux 41 case postale 80 ch-1705 fribourg switzerland tel: (41) 26-426-5555 fax: (41) 26-426-5500 asia room 1219 chinachem golden plaza 77 mody road tsimshatsui east kowloon hong kong tel: (852) 2721-9778 fax: (852) 2722-1369 japan 9f, tonetsu shinkawa bldg. 1-24-8 shinkawa chuo-ku, tokyo 104-0033 japan tel: (81) 3-3523-3551 fax: (81) 3-3523-7581 memory 2325 orchard parkway san jose, ca 95131, usa tel: 1(408) 441-0311 fax: 1(408) 436-4314 microcontrollers 2325 orchard parkway san jose, ca 95131, usa tel: 1(408) 441-0311 fax: 1(408) 436-4314 la chantrerie bp 70602 44306 nantes cedex 3, france tel: (33) 2-40-18-18-18 fax: (33) 2-40-18-19-60 asic/assp/smart cards zone industrielle 13106 rousset cedex, france tel: (33) 4-42-53-60-00 fax: (33) 4-42-53-60-01 1150 east cheyenne mtn. blvd. colorado springs, co 80906, usa tel: 1(719) 576-3300 fax: 1(719) 540-1759 scottish enterprise technology park maxwell building east kilbride g75 0qr, scotland tel: (44) 1355-803-000 fax: (44) 1355-242-743 rf/automotive theresienstrasse 2 postfach 3535 74025 heilbronn, germany tel: (49) 71-31-67-0 fax: (49) 71-31-67-2340 1150 east cheyenne mtn. blvd. colorado springs, co 80906, usa tel: 1(719) 576-3300 fax: 1(719) 540-1759 biometrics/imaging/hi-rel mpu/ high speed converters/rf datacom avenue de rochepleine bp 123 38521 saint-egreve cedex, france tel: (33) 4-76-58-30-00 fax: (33) 4-76-58-34-80 literature requests www.atmel.com/literature


▲Up To Search▲   

 
Price & Availability of ATTINY2507

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X